N = gets.to_i
hs = gets.split(' ').map(&:to_i)

cnt = 0
while !hs.all?(&:zero?) do
  chunks = hs.slice_before(&:zero?).map { |a| a.reject(&:zero?) }.select { |a| a.size > 0 }
  cnt += chunks.size

  chunks.each_with_index do |chunk, i|
    chunk = chunk.each_with_index do |k, j|
      chunks[i][j] -= 1
    end
  end

  hs = chunks.inject([]) { |ar, a| ar += a + [0] }
end

puts cnt