fun longList() = readLine()?.split(" ")?.map(String::toLong) ?: TODO()
fun main() {
    val n = readLine()?.toInt() ?: return
    val a = longList()
    var t = 0L
    var l = a[0]
    1.until(n).forEach {
        val b = a[it]
        if (l > b) {
            t += l - b
        } else {
            l = b
        }
    }
    println(t.toString())
}
