import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	} main {
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long a[] = new long[n]; a new long[n] n long max = 0; max 0 long chk = 0; chk 0 long min = 1000000000; min 1000000000 for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
		        a[i] = sc.nextLong();
		    } {
		        a[i] = sc.nextLong();
		    } a[i] = sc.nextLong() a[i] a i sc.nextLong() sc.nextLong sc for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    } {
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    } max = Math.max(max,a[i]) max Math.max(max,a[i]) Math.max Math max a[i] a i chk += max - a[i] chk max - a[i] max a[i] a i System.out.println(chk) System.out.println System.out System System.out chk String[] args args public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	}
} public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	}
} Main
import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		    int n = sc.nextInt();
		    long a[] = new long[n];
		    long max = 0;
		    long chk = 0;
		    long min = 1000000000;
		    for(int i=0;i<n;i++){
		        a[i] = sc.nextLong();
		    }
		    
		    for(int i=0;i<n;i++){
		        max = Math.max(max,a[i]);
		        chk += max - a[i];
		    }
		    System.out.println(chk);
	        
    
	}
}
