'use strict';

function Main(input) {
    // N A0~Ai
    const tmp = input.trim().split('\n');
    const N = Number(tmp[0]);
    const A = tmp[1].split(' ').map((v) => Number(v));

    let height = A[0];
    let result = 0;
    for (const a of A) {
        if (a > height) {
            height = a;
        }
        result += height - a;
    }
    console.log(result);
    return;
}
Main(require('fs').readFileSync('/dev/stdin', 'utf8'));