fun main(args: Array<String>) {
    val n = getInt()
    val a = getInts()
    var now = a[0]
    var sum = 0
    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() }