N = gets.to_i
h = gets.split(" ").map(&:to_i)
count = 0

while h != [] do
    while h[0] == 0 do
    h.shift 
    end
    h.size.times do |i|
        count += 1 if i == 0
        h[i] -= 1 if h[i] > 0
        break if h[i + 1] == 0
    end
end

puts count 