function Main(input) {
    tmp = input.split("\n");
    n = parseInt(tmp[0], 10);
    a = tmp[1].split(" ");
    a = a.map(Number);
    total = 0;


    for (let i = 0; i < n - 1; i++) {

      first = a[i];
      second = a[i+1];
      count = i;
      
       if (first > second) {
           total += first - second;
           a.splice(count+1, 1, first);
       }
    }

    console.log(total);
    



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