line = gets()
n = line.chomp.to_i

line = gets()
a = []
line.split(' ').each do |i|
    a.push(i.to_i)
end

max_h = 0
total = 0
a.each do |i|
    if i >= max_h
        max_h = i
        next
    end
    total += max_h - i
end
print(total)
