input = readlines()

n = input.shift.chomp.to_i
list = input.shift.chomp.split("\s").map{|x| x.to_i}

sum = 0

prev = list[0]
list.each_with_index do |l, i|
  next if i == 0
  if l >= prev
    prev = l
    next
  end
  diff = prev - l
  sum += diff
  prev = l + diff
end

puts sum
