fun main(args: Array<String>) {
    val n = next().toInt()
    val ta = nextLongList()

    var f =0L

    var r = 0L

    for(i in 0 until n){
        if(i==0) {
            f = ta[i]
            continue
        }

        if(ta[i] < f){
            r += f - ta[i]
        }else{
            f = ta[i]
        }

        
    }
  println(r)


}
fun next() = readLine()!!
fun nextIntList() = next().split(" ").map{ it.toInt() }
fun nextDoubleList() = next().split(" ").map{ it.toDouble() }
fun nextLongList() = next().split(" ").map{ it.toLong() }