fun main() {
    readLine()
    val a = readLine()!!.splitToSequence(" ").map(String::toLong).toList()
    var sum = 0L
    var previous = 0L
    for (ai in a) {
        if (previous < ai) {
            previous = ai
        } else {
            sum += previous - ai
        }
    }
    println(sum)
}