fun main() {
    val n = nextInt()
    val a = nextIntList()
    println(res(n, a))
}

fun res(n: Int, a: List<Int>): Int {
    var tmp = a[0]
    var result = 0
    for (i in 1 until n) {
        if (a[i] >= tmp) {
            tmp = a[i]
        } else {
            result += tmp - a[i]
        }
    }
    return result
}


fun next() = readLine()!!
fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun nextList() = next().split(' ')
fun nextIntList() = nextList().map { it.toInt() }
fun nextLongList() = nextList().map { it.toLong() }
fun nextDoubleList() = nextList().map { it.toDouble() }
