import java.math.BigDecimal

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

    aList.forEach { a ->
        base = maxOf(base, a)
        if (base > a) {
            sum = sum.add(BigDecimal(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, 200000, 3, 3, 3)

    val result = solver(n, aList)

    println(result.toString())
}
