import java.util.*;
public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	
	public static void main(String []args){
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	}
} import java.util.*; util.* java public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	
	public static void main(String []args){
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	}
} Main private static Scanner sc=new Scanner(System.in); sc new Scanner(System.in) public static void main(String []args){
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	} main {
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	} long n=sc.nextInt(); n sc.nextInt() sc.nextInt sc long curMax=0 curMax 0 temp temp totalHeight=0; totalHeight 0 for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		} {
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		} temp=sc.nextInt() temp sc.nextInt() sc.nextInt sc if(curMax<temp)
				curMax=temp; curMax<temp curMax temp curMax=temp curMax temp totalHeight+=(curMax-temp) totalHeight (curMax-temp) curMax temp System.out.println(totalHeight) System.out.println System.out System System.out totalHeight String []args args public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	
	public static void main(String []args){
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	}
} public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	
	public static void main(String []args){
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	}
} Main
import java.util.*;
public class Main{
	
	
	private static Scanner sc=new Scanner(System.in);
	
	public static void main(String []args){
		long n=sc.nextInt();
		long curMax=0,temp,totalHeight=0;
		for(int i=0;i<n;i++){
			temp=sc.nextInt();
			if(curMax<temp)
				curMax=temp;
			totalHeight+=(curMax-temp);
		}
		System.out.println(totalHeight);
	}
}