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

ans = 0
while h.count(0) != n
  prev = h.index{|v| v != 0}
  tail = h[prev..-1].index{|v| v==0}
  tail = tail == nil ? n : tail + prev

  ans += h[prev..(tail-1)].min
  h[prev..(tail-1)] = h[prev..(tail-1)].map{|v| v - h[prev..(tail-1)].min}
end

puts ans