fun main(args: Array<String>) {
    val n = getInt()
    val a = getLongs()
    var now = a[0]
    var sum = 0L
    a.drop(1).forEach {
        if (now > it) sum += (now - it)
        else now = it
    }

    println(sum)
}


fun getInt() = readLine()!!.toInt()
fun getInts() = readLine()!!.split(" ").map { it.toInt() }
fun getLong() = readLine()!!.toLong()
fun getLongs() = readLine()!!.split(" ").map { it.toLong() }