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