fun main() {

    val N = readLine()!!.toInt()

    val arrow = readLine()!!.split(' ').map { it.toInt() }

    var total = 0

    var maxMan = arrow[0]

    for(i in 0 until N) {
        if(maxMan > arrow[i]) {
            total += maxMan - arrow[i]
        } else {
            maxMan = arrow[i]
        }
    }

    print(total)
}