function main(input) {
  const array = input.split('\n')[1].split(' ');
  let sum = 0;
  array.forEach((one,index) => {
    if(index != 0){
      if(array[index - 1] > one){
        const tasu = array[index-1] - one;
        sum = sum + tasu;
        array[index] = array[index-1];
      }
    }
  })
  console.log(sum)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));