function Main(input) {
    input = input.trim().split("\n").map(function(x) { return x.split(" ")});
    let N = parseInt(input[0][0], 10);
    let v_A = input[1].map(e => parseInt(e, 10));
    let tmp = v_A[0];
    let ans = 0;
    for (let i = 1; i < N; i++){
        if (tmp <= v_A[i]) tmp = v_A[i];
        else ans += tmp - v_A[i];
    }
    console.log(ans);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));
