function main(input){
  input = input.split('\n');

  const N = Number(input[0]);
  const A = input[1].split(' ').map(Number);
  let sum = 0
  for(let i=1; i<N; i++){
    if(A[i-1] > A[i]){
      sum+= A[i-1] - A[i];
      A[i] = A[i-1];
    }
  }

  console.log(sum);
}

main(require('fs').readFileSync('/dev/stdin', 'utf8').trim());
