n = gets.chomp.to_i
hs = gets.chomp.split(" ").map(&:to_i)
count = 0
until hs.length == 0
  if hs.find_index {|i| i == 0} == 0
    hs.shift
  elsif hs.find_index {|i| i == 0} == nil
    hs.each_with_index do |h, index|
      hs[index] -= 1
    end
    count += 1
  else
    0.upto(hs.find_index {|i| i == 0} - 1).each do |index|
      hs[index] -= 1
    end
    count += 1
  end
end
puts count