import java.util.*

private val WHITESPACE = "\\s+".toRegex()

private fun readLn() = readLine()!!
private fun readList() = readLn().split(WHITESPACE)

// helps split strings into multiple tokens
private var tokenizer = StringTokenizer("")
private fun read(): String {
    while (tokenizer.hasMoreTokens().not()) tokenizer = StringTokenizer(readLn(), " ")
    return tokenizer.nextToken()
}

private fun readInt() = read().toInt()
private fun readLong() = read().toLong()
private fun readDouble() = read().toDouble()

private fun readIntList() = readList().map { it.toInt() }
private fun readLongList() = readList().map { it.toLong() }
private fun readDoubleList() = readList().map { it.toDouble() }

private fun readIntArray(n: Int = 0) =
    if (n == 0) readList().run { IntArray(size) { get(it).toInt() } } else IntArray(n) { readInt() }

fun main() {
    val n = readInt()
    val p = readIntList()

    var h = 0
    var max = 0
    for (i in p) {
        max = kotlin.math.max(max, i)
        h += max - i
    }

    println(h)
}