n = gets.chomp.to_i
ary = gets.chomp.split.map(&:to_i)

def find_non_zero_range(ary)
  s = ary.index(&:nonzero?)
  if s
    e = (ary[(s + 1)..-1].index(&:zero?) + s) rescue (ary.size - 1)
    return s..e
  else # all zero
    return nil
  end
end

ans = 0
loop do
  target_rng = find_non_zero_range(ary) || break
  min = ary[target_rng].min
  target_rng.each do |i|
    ary[i] = ary[i] - min
  end
  ans += min
end
puts ans