fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val aList = readLine()!!.split(' ').map(String::toLong)
    var tempMax = aList[0]
    var ans = 0L
    for (i in 1 until aList.size) {
        ans += Math.max(0, tempMax - aList[i])
        tempMax = Math.max(tempMax, aList[i])
    }
    println(ans)
}
