n = gets.to_i
array = gets.split.map(&:to_i)
array_diff = []

n.times do |i|
  if i == 0
    array_diff.push(0)
  else
    diff = array[i] - array[i - 1]
    if diff < 0
      array[i] += diff.abs
      array_diff.push(diff.abs)
    else
      array_diff.push(0)
    end
  end
end

puts array_diff.sum
