fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    var f = 0
    var res = 0
    val iter = readLine()!!.split(" ").map { it.toInt() }
    for (i in iter) {
        if (f <= i) {
            res += f - i
            f = i
        } else {
            f = i
        }
    }
    println(res)
}