function MainC(input){
    let [[N], [...Ai]] = input.split('\n').map(a=>a.split(' ').map(Number));
    let answer = 0;
    for(let i=0; i<N-1; i++){
        let box = Ai[i+1] - Ai[i];
        if(box < 0) {
            answer += -box;
            Ai[i+1] += -box; 
        }
    }
    console.log(answer);
}
MainC(require("fs").readFileSync("/dev/stdin", "utf8").trim());
