n = gets.to_i
as = gets.chomp.split(" ").map(&:to_i)
totalStep = 0
after = as[1]
renewdAfter = 0
newAs = as
isAfterRenewed = false
as.each_with_index do |a, i|
    
    isAfterRenewed ? after = renewdAfter : after = as[i + 1]  
    
    if i == (as.length - 1)
        step = newAs[as.length-2] - a
        if step > 0
            totalStep += step
            newAs[i] = a + step
        end
        next
    end
    
    if newAs[i]> after
        step = a - after
        totalStep += step
        isAfterRenewed = true
        renewdAfter = after + step
        newAs[i+1] = renewdAfter
    else
        isAfterRenewed = false
    end

end
print totalStep
