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 = Int.MIN_VALUE
    var total : Long = 0
    for(i in 0..arr.lastIndex)
    {
        max = Math.max(max,arr[i])
        total += max - arr[i]

    }
    println(total)
}
