io = STDIN
n=io.gets.to_i
h=io.gets.split.map(&:to_i)
def calc(h)
  ix=h.find_index{|a|a>0}||0
  h=h[ix..-1].reverse
  ix=h.find_index{|a|a>0}||0
  h=h[ix..-1].reverse
  size=0
  h.chunk do |i|
    i>0
  end.each do |plus,ar|
    next unless plus
    min=ar.min
    size+=min
    ar.map!{|n|n-min}
    size+=calc(ar)
  end
  size
end
puts calc(h)
