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

humidai = []
highest = al.first
al.each do |a|
  if a < highest
    humidai.push(highest - a)
  end
  if a > highest
    highest = a
  end
end

puts humidai.sum