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

lp = 0
while true
    # 水やりの始点と終点を決める
    left = h.index{|i| i > 0 }
    if left.nil?
        # 水やり終了
        break
    end

    right = nil
    for i in left..h.length-1
        if h[i] == 0
            right = i - 1
            break
        end
    end
    if right.nil?
        right = h.length-1
    end

    d = h[left..right].min
    for j in left..right
        h[j] -= d
    end
    count += d

    break if lp > 10
    lp += 1
end

puts count
