//let input = "5\n3 3 3 3 3";

function main(input){
  input = input.trim();
  let lines = input.split("\n");
  //console.log(lines);
  let list = lines[1].split(" ");
  let count = 0;
  for(let i=0; i<list.length; i++){
    list[i] = parseInt(list[i]);
    if(i !== 0){
      let fumidai = list[i-1] - list[i];
      if(fumidai < 0){fumidai = 0;}
      list[i] += fumidai;
      count += fumidai;
    }
  }
  console.log(count);
}

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