n = gets.to_i
a = gets.split.map(&:to_i)
max = 0
height = 0
for i in 0..n-1 do
    if a[i] < max then
        height = height + max - a[i] 
        max = max
    else
        max = a[i]
    end
end
puts height