import java.util.Scanner;
public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    }
} Main public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    } main {
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[]a = new int[n]; a new int[n] n int[]A = new int[n]; A new int[n] n long al = 0; al 0 for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	} int i=0; int i=0; i 0 i<n i n i++ i++ i {
    		a[i] = sc.nextInt();
    		al += a[i];
    	} {
    		a[i] = sc.nextInt();
    		al += a[i];
    	} a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc al += a[i] al a[i] a i long all= a[0]; all a[0] a 0 for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	} int i=1; int i=1; i 1 i<n i n i++ i++ i {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	} {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	} if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		} a[i] < a[i-1] a[i] a i a[i-1] a i-1 i 1 {
    			a[i] = a[i-1];
    			all += a[i];
    		} a[i] = a[i-1] a[i] a i a[i-1] a i-1 i 1 all += a[i] all a[i] a i {
    			all += a[i];
    		} all += a[i] all a[i] a i System.out.println(all - al) System.out.println System.out System System.out all - al all al String[] args args public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    }
} public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[]a = new int[n];
    int[]A = new int[n];
    
    long al = 0;
    for(int i=0; i<n; i++) {
    		a[i] = sc.nextInt();
    		al += a[i];
    	}
    	long all= a[0];
    	for(int i=1; i<n; i++) {
    		if(a[i] < a[i-1]) {
    			a[i] = a[i-1];
    			all += a[i];
    		}else {
    			all += a[i];
    		}
    	}
    	System.out.println(all - al);
    }
}