fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(' ').map { it.toInt() }.toMutableList()
    var sum = 0
    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)
}