fun main() {

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

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

    var total = 0

    for(i in 1 until N) {
        if(arrow[i - 1] > arrow[i]) {
            (arrow[i - 1] - arrow[i]).let {
                total += it
                arrow[i] += it
            }
        }

    }

    print(total)
}