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

steps = []
h = A[0]
N.times do |i|
    if h > A[i] then
        steps[i] = h - A[i]
    else
        steps[i] = 0
        h = A[i]
    end
end

puts steps.sum