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