import java.util.*

fun main(args: Array<String>) {
    val scan = Scanner(System.`in`)
    var kotlin=scan.nextLine().toInt()
    var arr = scan.nextLine().split(" ").map{ it.trim().toInt() }.toTypedArray()

    var prev = arr[0]
    var max = 0
    var total = 0
    for(i in 1..arr.lastIndex)
    {
        max = Math.max(max,arr[i])

        if(prev > arr[i])
        {

            total += Math.max(prev - arr[i],(max - arr[i]))
        }

        prev = arr[i]

    }
    println(total)
}
