N = gets.to_i
HEIGHTS = gets.split(' ').map(&:to_i)

sum_steps = 0
max_heigtht = HEIGHTS[0]
HEIGHTS.each_with_index do |height, i|
  if max_heigtht > height
    sum_steps += max_heigtht - height
  elsif max_heigtht < height
    max_heigtht = height
  end
end
puts sum_steps
