import kotlin.math.max

fun main() {
    val nk = nextLong()
    val an = nextLongList()
    val ans = solve(an)
    println(ans)


}


fun solve(k: List<Long>): Long {
    return step(k)
}

fun step(height: List<Long>): Long {
    tailrec fun loop(tempMax: Long, stepSum: Long, index: Int): Long {
        return if (height.size == index) {
            stepSum
        } else {
            val newStepSum = if (tempMax > height[index]) stepSum + (tempMax - height[index]) else stepSum
            val newTempMax = max(tempMax, height[index])
            loop(newTempMax, newStepSum, index + 1)
        }
    }
    return loop(0L, 0L, 0)
}


fun nextInt(): Int {
    return readLine()!!.toInt()
}

fun nextIntList(): List<Int> {
    return readLine()!!.split(" ").map { it.toInt() }
}
fun nextLong(): Long {
    return readLine()!!.toLong()
}

fun nextLongList(): List<Long> {
    return readLine()!!.split(" ").map { it.toLong() }
}

fun nextString(): String {
    return readLine()!!
}