fun main() {
    readLine()
    var max = 0L
    var cnt = 0L
    readLine()!!.split(" ").map(String::toLong).forEach {
        if (it < max) {
            cnt += (max - it)
        } else {
            max = it
        }
    }
    println(cnt)
}
