fun main(){

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

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

    var cnt = 0

    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)

}