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

prev = nil
sum = 0
a_list.each do |a|
    if !prev.nil? && prev > a
        diff = prev - a
        sum += diff
    else
        prev = a
    end
end
puts sum