process.stdin.resume();
process.stdin.setEncoding('utf8');

var lines = [];
var reader = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});
reader.on('line', (line) => {
  lines.push(line);
});
reader.on('close', () => {
  var N = parseInt(lines[0]);
  var ALength = lines[1].split(' ').length;

  var sum = 0;
  var h = parseInt(lines[1].split(' ')[0]);
  var springboardHeight = 0;
  for (var i = 1; i < ALength; i++) {
    var aCurrent = parseInt(lines[1].split(' ')[i]);
    if (h > aCurrent) {
      springboardHeight = h - aCurrent;
    } else {
      h = aCurrent;
    }
    sum += springboardHeight;
  }
  
  console.log(sum);
});