n = gets.to_i
n_loop = n - 1

a = gets.chomp.split(" ").map!{|i|i.to_i}

diff_total = 0
n_loop.times do |i|
  next_a = a[i+1]
  diff = a[i+1] - a[i]
  if diff < 0
  	a[i+1] = a[i]
    diff_total += diff.abs
  end
  
end

p diff_total