fun main() {
    // 文字列
//    var n = readLine()!!.toInt()
//    val x: String = readLine()!!
    // 数値
    val n =readLine()!!.toInt()
    // 行
//    val list = readLine()!!.split(" ").map (String::toInt )

//    var list = readLine()!!.split("").map(String::toInt)
    var list = readLine()!!.split(" ").map(String::toLong).toMutableList()
//    val xys = List(n) {
////        readLine()!!.split(" ").map(String::toLong)
//        readLine()!!.toDouble()
//    }
    var count=0L
    for (ai in 0 until n-1){
        if (list[ai]<=list[ai+1]){
            //何もしない
        }else{
            count=count+list[ai]-list[ai+1]
            list[ai+1]=list[ai]
        }
    }
    print(count)

}
