fun main(args:Array<String>) {
    val n = readLine()!!.toInt()
    val input = readLine()!!.split(" ").map { it.toInt() }
    var tmp = input[0]
    var up = 0
    for (i in input) {
        if (tmp <= i) {
            tmp = i
            continue
        }
        up += tmp - i
        tmp = i
    }
    println(up)
}