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

max = 0
count = 0

array.each_with_index do |arr, i|
  if i == 0
    max = arr
  else
    if max > arr
      count += (max - arr)
    elsif max < arr
      max = arr
    end
  end
end

puts count
