def watering( flws )
  ret = 0
  flws.each do |a|
    m = a.min
    ret += m
    a.map!{|x|x-m}
    b = []
    c = []
    a.each do |x|
      if x != 0
        c << x
      else
        if c.size > 0
          b << c
          c = []
        end
      end
    end
    if c.size > 0
      b << c
    end
    if b.size > 0
    	ret += watering( b )
    end
  end
  ret
end

n = STDIN.gets.to_i
h = STDIN.gets.split(/ +/).map(&:to_i)

puts watering( [h] )

