n = gets.chomp.to_i
arr = gets.chomp.split(' ').map(&:to_i)
i = 1
basic = arr[0]
sum = 0
while i < n
  if basic > arr[i] # 2 1
    sum += basic-arr[i]
  else # 2 5
    basic = arr[i]
  end
  i += 1
end
puts sum