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

steps = []
max_heigtht = 0
HEIGHTS.each_with_index do |height, i|
  if i == 0
    max_heigtht = height
  else
    if max_heigtht > height
      steps.push(max_heigtht - height)
    else
      max_heigtht = height
    end
  end
  pp "max_height: #{max_heigtht}, max_step: #{steps}"
end
puts steps.sum
