n=gets.to_i
h=gets.split.map &:to_i
cnt=0

while true
    l=h.index{|i|i!=0}
    k=h[l...n].index(0)
    if !l then break end
    if k!=nil
        k+=l
    else
        k=n
    end
    mn=h[l...k].min
    cnt+=mn
    h[l...k]=h[l...k].map{|i|i-mn}
    break if h.all?{|i|i==0}
end

p cnt