n = gets.to_i
flowers = gets.split.map(&:to_i)
ans = 0
until flowers.max == 0
  ans += 1
  from = flowers.find_index { |n| n > 0 }
  to = flowers.find_index.with_index { |n, i| n == 0 && i > from } || n
  (from...to).each do |i|
    flowers[i] -= 1
  end
end
puts ans
