import * as fs from 'fs';

function main(input) {
  input = input.split("\n");
  const N = parseInt(input[0], 10);
  const AList = input[1].split(" ").map(i => parseInt(i, 10));
  let count = 0;
  for (let i = 1; i < N; i++) {
    count += Math.max(0, AList[i - 1] - AList[i]);
  }

  console.log('%d', count);
  return 0;
}

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