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