N = gets.to_i
Hs = gets.split.map(&:to_i)

def cut(hs, acc)
  i, a = hs.minmax
  return acc if a.zero?
  return cut(hs.map{|h| h - i}, acc + i) if i > 0
  hs.chunk(&:positive?).select(&:first).map(&:last).inject(acc){|s, a| s + cut(a, 0)}
end

puts cut(Hs, 0)