fun main(){
    val N = readLine()!!.toLong()
    var A = readLine()!!.split(" ").map { it.toLong() }.toMutableList()

    var total: Long = 0

    for(i in 1 until (A.size)){
        if(A[i] < A[i-1]){
            total += A[i-1] - A[i]
            A[i] += A[i-1] - A[i]
        }
    }
    println(total)
}