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

(1 .. 10_000).find do
  l = nil
  r = nil
  w = INF
  
  H.each_with_index.find do |v,i|
    if l.nil?
      if v > 0
        w = v
        l = i
      end
      false
    else
      if v == 0
        r = i
      else
        w = v < w ? v : w
        false
      end
    end
  end
  r = N if r.nil?
  
  (l ... r).each{|i| H[i] -= w }
  ans += w
  
  H.all?(&:zero?)
end
puts ans