import java.util.Scanner;


	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 ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    }
				
	}
	 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();
			long[] a = new long[n];
			long max = 0;
			long ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    }
				
	} 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 ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    } main {

			Scanner sc = new Scanner(System.in);
			
			int n = sc.nextInt();
			long[] a = new long[n];
			long max = 0;
			long ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    } 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 ret = 0; ret 0 for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			} int i=0; int i=0; i 0 i<n i n i++ i++ i {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			} {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			} a[i] = sc.nextLong() a[i] a i sc.nextLong() sc.nextLong sc max = Math.max(max, a[i]) max Math.max(max, a[i]) Math.max Math max a[i] a i if(max>a[i]) {
					ret += (max-a[i]);
				} max>a[i] max a[i] a i {
					ret += (max-a[i]);
				} ret += (max-a[i]) ret (max-a[i]) max a[i] a i System.out.println(ret) System.out.println System.out System System.out ret 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 ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    }
				
	} 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 ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    }
				
	} Main
import java.util.Scanner;


	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 ret = 0;
			
			for(int i=0;i<n;i++) {
				a[i] = sc.nextLong();
				max = Math.max(max, a[i]);
				if(max>a[i]) {
					ret += (max-a[i]);
				}
			}
			System.out.println(ret);
			
			
	    }
				
	}
	
