import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class Main
{
	
	public static ArrayList<String> AL = new ArrayList<String>();
	
	
	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	}
		
	
	  
	static final Random random=new Random();
	
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.Random; Random java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main
{
	
	public static ArrayList<String> AL = new ArrayList<String>();
	
	
	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	}
		
	
	  
	static final Random random=new Random();
	
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
} Main public static ArrayList<String> AL = new ArrayList<String>(); AL new ArrayList<String>() static final Random random=new Random(); random new Random() public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	} main {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	} FastScanner fs=new FastScanner(); fs new FastScanner() int n = fs.nextInt(); n fs.nextInt() fs.nextInt fs long a[] = fs.readArray(n); a fs.readArray(n) fs.readArray fs n long count =0 ; count 0 for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		} int i =0 ; int i =0 ; i 0 i<n-1 i n-1 n 1 i++ i++ i {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		} {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		} if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			} a[i]>a[i+1] a[i] a i a[i+1] a i+1 i 1 {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			} count+=a[i]-a[i+1] count a[i]-a[i+1] a[i] a i a[i+1] a i+1 i 1 a[i+1] = a[i] a[i+1] a i+1 i 1 a[i] a i System.out.println(count) System.out.println System.out System System.out count String[] args args static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	} ruffleSort {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	} int n=a.length; n a.length a a.length for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		} {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		} int oi=random.nextInt(n) oi random.nextInt(n) random.nextInt random n temp=a[oi]; temp a[oi] a oi a[oi]=a[i] a[oi] a oi a[i] a i a[i]=temp a[i] a i temp Arrays.sort(a) Arrays.sort Arrays a int[] a a static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	} FastScanner BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st=new StringTokenizer(""); st new StringTokenizer("") String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} next {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		} while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} IOException e {
					e.printStackTrace();
				} e.printStackTrace() e.printStackTrace e {
					st=new StringTokenizer(br.readLine());
				} st=new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		} readArray {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		} long[] a=new long[n]; a new long[n] n for (int i=0; i<n; i++) a[i]=nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=nextInt(); a[i]=nextInt() a[i] a i nextInt() nextInt return a; a int n n long nextLong() {
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main
{
	
	public static ArrayList<String> AL = new ArrayList<String>();
	
	
	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	}
		
	
	  
	static final Random random=new Random();
	
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
} public class Main
{
	
	public static ArrayList<String> AL = new ArrayList<String>();
	
	
	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	}
		
	
	  
	static final Random random=new Random();
	
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;

public class Main
{
	
	public static ArrayList<String> AL = new ArrayList<String>();
	
	
	public static void main(String[] args) {
		FastScanner fs=new FastScanner();
		int n = fs.nextInt();
		long a[] = fs.readArray(n);
		long count =0 ;
		for(int i =0 ;i<n-1;i++) {
			
			if(a[i]>a[i+1]) {
				count+=a[i]-a[i+1];
				a[i+1] = a[i];
			}
		}
		
		System.out.println(count);
	}
		
	
	  
	static final Random random=new Random();
	
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		
		int nextInt() {
			return Integer.parseInt(next());
		}
		long[] readArray(int n) {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}

	
}
