import * as fs from "fs";

const input = fs
  .readFileSync("/dev/stdin", "utf8")
  .split("\n")[1]
  .split(" ")
  .map(Number);
let ans = 0;
let max = input[0];
input.forEach((x,i) => {
  // 後ろの値が小さければ追加の処理;
  if(input[i] < max ) {
    console.log(input[i] max);
    ans +=  max - input[i];
  }else {
    // そうじゃない時は値を更新
  	max = input[i]
  }
});
console.log(ans);