fun solver(n: Int, aList: List<Int>): Int {
    var sum = 0
    var base = 0

    aList.forEach { a ->
        base = maxOf(base, a)
        if (base > a) {
            sum += base - a
        }
    }


    return sum
}

fun main(args: Array<String>) {
    val n =  readLine()!!.toInt()
    val aList = readLine()!!.split(" ").map { it.toInt() }

/*
    val n = 5
    val aList = listOf(3, 3, 3, 3, 3)
    
 */

    val result = solver(n, aList)

    println(result)
}
