n = gets.chomp.to_i
h = gets.chomp.split(' ').map(&:to_i)
ans = 0
while(true) do
  min = 10 ** 7
  l = -1
  r = -1
  h.each_with_index do |v, i|
    if l == -1 && v > 0
      l = i
    elsif l != -1 && r == -1 && v == 0
      r = i - 1
    end
    min = [v, min].min if v != 0
  end
  break if r == -1 && l == -1
  if r == -1
    r = n - 1
  end
  l.upto r do |i|
    h[i] -= min
  end
  ans += min
end
puts ans
