N = gets.to_i
ary = gets.split.map(&:to_i)
humidai = 0
last_height = 0
ary.each do |a|
    if last_height > a
        humidai += last_height - a
    else
        last_height = a
    end
end
puts humidai