n = gets.chomp.to_i
a = gets.chomp.split(' ').map(&:to_i)
result = []
max = -1
for i in 0...n
  if (i == 0)
    result << 0
    max = a[i]
    next
  end
  if (a[i] <= max)
    result << max - a[i]
  else
    result << 0
    max = a[i]
  end
end
puts result.sum