function Main(input) {
    tmp = input.split("\n");
    n = parseInt(tmp[0], 10);
    a = tmp[1].split(" ");
    a = a.map(Number);
    total = 0;

    while (a.length > 0) {
        max = Math.max.apply(null, a); // 5
        num = a.indexOf(max); // 2
    
        for (let i = num + 1; i < a.length; i++) {
            total += max - a[i];
        }
        a.splice(num, a.length);

    }    

    console.log(total);


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