N = gets.to_i
hs = gets.split.map(&:to_i)
$ans = 0

def zero_partition(x)
    list = []
    tmp = []
    x.each{|num|
        if num != 0
            tmp.push(num)
        else
            list.push(tmp) if tmp != []
            tmp = []
        end
    }
    list.push(tmp) if tmp != []
    return list
end

def loop(hs)
    min = hs.min
    tmp = hs.map{|x| x - min}
    $ans = $ans + min
    target = zero_partition(tmp)
    if target != []
        target.each{|arr|
            loop(arr)
        }
    end
end
loop(hs)
puts $ans
