n = STDIN.readline.to_i
h_list = STDIN.readline.split().map{|h|h.to_i}
sum = 0
for i in Range.new(1, h_list.max) do
    counts = 0
    prev = 0
    for j in Range.new(0, n, true) do
        if h_list[j] > 0 then
            if prev == 0 then
                counts += 1
            end
            prev = 1
            h_list[j] -= 1
        else
            sum += counts
            counts = 0
            prev = 0
        end
    end
    sum += counts
end

puts(sum)