var readline = require('readline');
var rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});

rl.on('line', function (line) {
    const arr = line.split(' ').map(x => Number(x));
    let sum = 0;
    let j =0;
    currentBig = arr[arr.length - 1]; 
    for (let i = arr.length-1; i>0; i--) {
        let diff = 0
        if( arr[i] < arr[i-1] ) {
            diff = (arr[i - 1] - arr[i]);
            sum += diff;
        }

        if (currentBig < arr[i - 1]) {
            currentBig = arr[i - 1];
            sum += diff*j;
        }
        j++;
    }
    console.log(sum);
})
