fun main() {
    readLine()!!
    val list = readLine()!!.split(" ").map { it.toInt() }
    var temp = list[0]
    var count = 0
    for (a in list.indices) {
        if (list[a] <= temp) {
            count += temp - list[a]
        } else {
            temp = list[a]
        }
    }
    println(count)
}