def split(heights)
  res = []
  this = []
  h = heights.dup
  until h.empty?
    if h[0] == 0
      unless this.empty?
        res << this
        this = []
      end
    else
      this << h[0]
    end
    h.shift
  end
  unless this.empty?
    res << this
  end
  res
end

def solve(spans)
  spans.inject(0) do |acc, span|
    minimum = span.min
    acc + minimum + solve(split(span.map { |n| n - minimum }))
  end
end

n = gets.to_i
heights = gets.split.map(&:to_i)

p solve(split(heights))
