function Main(input) {
  input = input.split("\n");
  const N = input[0];
  const heights = input[1].split(" ");
  
  var steps = 0;
  
  for(var i = 1; i < N ; i++){
    var a = parseInt(heights[i-1], 10);
    var b = parseInt(heights[i], 10);
  	if(b < a){
    	var dif = a - b;
    	steps += dif;
      	heights[i] = parseInt(heights[i], 10) + dif;
    }
  }
  
  console.log("%d", steps);
}

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