import kotlin.math.max

fun main() {
    val nk = nextInt()
    val an = nextIntList()
    val ans = solve(an)
    println(ans)


}


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

fun step(height: List<Int>): Int {
    tailrec fun loop(tempMax: Int, stepSum: Int, index: Int): Int {
        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(0, 0, 0)
}


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

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

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

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