function main(arg) {
    
    let n = Number(arg.split("\n")[0])
    let a = arg.split("\n")[1].split(" ").map(x=>Number(x))
    
    let i=0, max=a[i], sum=0
    for(i=1; i<n; i++) {
        sum += Math.max(max-a[i], 0)
        max = Math.max(max, a[i])
    }
    console.log(sum)
}

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