N = gets.chomp.to_i
As = gets.split(' ').map(&:to_i)

total = 0
step = 0

As.each_with_index do |a, index|
    next if index == 0

    prev_height = As[index - 1] + step
    if prev_height > a
        step = prev_height - a
        total += step
    else
        step = 0
    end
end

puts total