num = gets.to_i
high = gets.split(" ").map(&:to_i)

cost = 0

(high.length - 1).times do |i| 
    if(high[i] > high[i + 1])
      diff = high[i] - high[i + 1]
      cost += diff
      high[i + 1] += diff
    end
end

puts cost