console.log((args=>{
  const [[N],A] = args.trim().split`\n`.map(r=>r.split` `.map(v=>v|0));
  let s = 0, max = A[0];
  for ( let i = 1; i < N; i++ ) if ( A[i] < max ) {
    s += max - A[i];
  } else max = A[i];
  return s;
})(require('fs').readFileSync('/dev/stdin','utf8')));
