fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toLong() }.toLongArray()
    var extra = 0L
    if (n > 1) {
        for (i in 1..a.lastIndex) {
            if (a[i] < a[i - 1]) {
                val needed = a[i - 1] - a[i]
                a[i] += needed
                extra += needed
            }
        }
    }
    println(extra)
}