







fun main() {
    val n = readLine()!!.trim().toInt()
    val heights = readLine()!!.trim().split(' ').map(String::toInt).toIntArray()
    var result = 0L
    for (i in 1 until n) {
        if (heights[i] < heights[i - 1]) {
            result += heights[i - 1] - heights[i]
            heights[i] = heights[i - 1]
        }
    }
    println(result)
}