fun main(){
    val n = readLine()!!.toLong()
    var tmpMax = 1L
    readLine()!!.split(" ").map(String::toLong).fold(0L){ accum, value ->
        val diff = tmpMax - value
        if(diff < 0){
            tmpMax = value
            accum
        } else {
            accum + diff
        }
    }.let { print(it) }
}