
function main(input = "") {
    let lines = input.split("\n");
    let N = 0 | lines[0];
    let arr = lines[1].split(" ").map((v) => (Number(v)));

    let res = 0;
    for (let i = 1; i < N; i++) {
        let diff = arr[i - 1] - arr[i];
        if (diff > 0) {
            res += diff
            arr[i] += diff;
        }
    }
    return res;
}

//******************************
exports.main = main;

function Main(input) {
    console.log(main(input.trim()));
}

if (process.argv[2] !== "test") {
    Main(require("fs").readFileSync("/dev/stdin", "utf8"));
}