n = gets.chomp.to_i
a = gets.chomp.split
height = 0
min = 0
a[0] = a[0].to_i
(n-1).times do |i|
    a[i+1] = a[i+1].to_i
    min = a[i] if a[i] > min
    diff = a[i+1] - a[i]
    if diff < 0 || a[i+1] < min
        height += a[i+1] + diff.abs > min ? diff.abs : (a[i+1] - min).abs
    end
end
puts height
