(async () => {
    let input = '';
    for await (const chunk of process.stdin) input += chunk;
    let heights = input.split(/\s/).map(v => +v);
    heights = heights.slice(0, heights.shift());

    let min = heights[0];
    let sum = 0;
    for (const heght of heights) {
        sum += Math.max(0, min - heght);
        min = Math.max(min, heght);
    }
    console.log(sum);
})();
