function Main(input) {
  const lines = input.split('\n');
  const N = lines[0];
  const A = lines[1].split(' ').map(i => parseInt(i));
  let prevHeight = 0;
  let steps = 0
  A.forEach((a, i) => {
    if (i === 0) {
      prevHeight = a;
    } else {
      if (prevHeight < a) {
        prevHeight = a;
      } else {
        steps += (prevHeight - a);
        prevHeight = prevHeight
      }
    }
  });
  console.log(steps);
}

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