function Main(input) {
  	const input_split = input.split("\n");
  	let member_shincho = input_split[1].split(" ")
    member_shincho = member_shincho.map(x => parseInt(x))
  	let member_shincho2 = member_shincho
    
    let output = 0
    
    for(i=1;i<member_shincho.length;i++){
    	if(member_shincho[i-1] > member_shincho[i]){
          output += member_shincho[i-1] - member_shincho[i]
          
          member_shincho[i] = member_shincho[i-1]
          
        }
    }
       
    
    
	console.log(output);
}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));