time = gets.chomp.to_i
contents = gets.chomp.split.map(&:to_i)
ans = 0
time.times do |num|
  next if num == 0 || contents[num] > contents[num - 1]
  ans += contents[num - 1] - contents[num]
  contents[num] = contents[num - 1]
end
puts ans 
