INF = 10_000_000
N = gets.to_i
H = gets.split.map(&:to_i)
ans = 0

head = Array.new(N)
ranges = {}

(1 .. 10_000).find do
  head.clear
  ranges.clear
  
  H.each_index do |i|
    if head[i - 1] && H[i] > 0
      head[i] = head[i - 1]
    elsif head[i - 1].nil? && H[i] > 0
      head[i] = i
    end
  end
  
  head.each_with_index do |head, tail|
    ranges[head] = tail if head
  end
  
  ranges.each do |head, tail|
    w = H[head .. tail].min
    (head .. tail).each{|i| H[i] -= w}
    ans += w
  end

  H.all?(&:zero?)
end
puts ans