n = gets.to_i
array = gets.split(" ").map(&:to_i)

ans = 0
(n-1).times do |i|
#   p "#{array[i]} #{array[i+1]}"
  if array[i] > array[i+1]
    diff = array[i] - array[i+1]
    array[i+1] += diff
    ans += diff
  end
end

puts ans