n = gets.to_i
height_array = gets.split(" ").map(&:to_i)

cntStand = 0

(1...n).each do |i|
  if height_array[i-1] > height_array[i]
    cntStand += height_array[i-1] - height_array[i]
    height_array[i] = height_array[i-1]
  end
end

puts cntStand