fun main(){
    val n = readLine()!!.split(" ")[0].toInt()
    val a = readLine()!!.split(" ").map { it.toInt() }.toMutableList()
    var total = 0L
    for (i in 1 until n){
        if (a[i] < a[i - 1]){
            val x = a[i - 1] - a[i]
            total += x
            a[i] += x
        }
    }
    println(total)

}