#!/usr/bin/env ruby
num = gets.chomp
inputs = gets.chomp.split(' ').map(&:to_i)
result = []
ans = 0

inputs.each_with_index do | input, idx |
  if idx != 0
    if input < result[idx - 1]
      result << result[idx - 1]
    else
      result << input
    end
  else
    result << input
  end
end

result.each_with_index do | r, idx |
  ans += r - inputs[idx]
end

puts ans
