fun main(){

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

    var list = readLine()!!.split(" ").map(String::toLong).toMutableList()

    var cnt = 0L

    for (i in 0..list.size-2){
        if (list[i]>list[i+1]){
            cnt += list[i] - list[i+1]
            list[i+1] = list[i]
        }
    }

    println(cnt)
}