import java.util.*
fun main(args: Array<String>) {
    val reader = Scanner(System.`in`)

    val N = reader.nextInt()
    var maxNum = 0L
    var sumNum = 0L
    repeat(N){
        val currentNum = reader.nextLong()
        if(it == 0){
            maxNum = currentNum
        }
        else {
            if(currentNum < maxNum){
                sumNum += maxNum - currentNum
            }
            else {
                maxNum = currentNum
            }
        }
    }
    
    println(sumNum)
}