fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(' ').map { it.toLong() }.toMutableList()
    var sum = 0L
    repeat(n - 1) {
        sum += if(a[it] > a[it + 1]) {
            val tmp = a[it] - a[it + 1]
            a[it + 1] += tmp
            tmp
        } else 0
    }
    print(sum)
}