func readInts() -> [Int] {
    return readLine()!.split(separator: " ").map { Int($0)! }
}

func main() {
    readLine()!
    let arr = readInts()
    var result = 0
    var prev = -1
    for x in arr {
        if prev == -1 {
            prev = x
            continue
        }
        if prev > x {
            result += prev - x
        } else {
            prev = x
        }
    }
    print("\(result)")
}

main()
