t = gets.chomp.to_i
input = gets.chomp.split(" ")
input.map!(&:to_i)
deff = 0
(0..t-2).each do |num|
    if input[num+1] < input[num]
        deff += input[num]-input[num+1]
        input[num+1] = input[num]
        
    end
end

puts deff
