function Main(input) {
	let[,Arr] = input.split('\n')
    Arr = Arr.split(' ').map(e=>Number(e))
  	const temp = Arr.map((e,i)=>{
    	if(i===0){return 0}
      	if(Arr[i-1]===e){
          return 0
        }else if(e<Arr[i-1]){
        	let diff = Arr[i-1]-e
            Arr[i] = Arr[i]+diff
            return diff
        }else{
        	return 0
        }
    })
  console.log(temp.reduce((a,b)=>a+b))
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));