

private fun getLongs(): LongArray {
    return readLine()!!.split(" ").map {it.toLong()}.toLongArray()
}


fun main(){
    val ns = getLongs()
    val arr = getLongs()
    var currentMaxHeight = 0L
    var stand = 0L
    for (i in arr.indices) {
        if (arr[i]<currentMaxHeight) {
            stand += currentMaxHeight - arr[i]
        }
        if (arr[i]>currentMaxHeight) {
            currentMaxHeight = arr[i]
        }
    }
    print(stand)
}