import java.io.*;
import java.util.*;

public class Main {
	
	static long mod= (long) 1e9+7;
	
		
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	}

}


class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
        reader = new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt"));
        tokenizer = new StringTokenizer("");
    }
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
        return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
} import java.io.*; io.* java import java.util.*; util.* java public class Main {
	
	static long mod= (long) 1e9+7;
	
		
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	}

} Main static long mod= (long) 1e9+7; mod (long) 1e9+7 (long) 1e9 7 public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	} main {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	} Reader.init(System.in) Reader.init Reader System.in System System.in PrintWriter out=new PrintWriter(System.out); out new PrintWriter(System.out) int n=Reader.nextInt(); n Reader.nextInt() Reader.nextInt Reader int[] arr=new int[n]; arr new int[n] n long ans=0; ans 0 int max=0; max 0 for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		} {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		} int a=Reader.nextInt(); a Reader.nextInt() Reader.nextInt Reader if(max>a) {
				ans+=(max-a);
			} max>a max a {
				ans+=(max-a);
			} ans+=(max-a) ans (max-a) max a max=Math.max(a,max) max Math.max(a,max) Math.max Math a max out.println(ans) out.println out ans out.flush() out.flush out String[] args args class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
        reader = new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt"));
        tokenizer = new StringTokenizer("");
    }
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
        return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
} Reader static BufferedReader reader; reader static StringTokenizer tokenizer; tokenizer /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
        reader = new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt"));
        tokenizer = new StringTokenizer("");
    } init {
        reader = new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt"));
        tokenizer = new StringTokenizer("");
    } reader = new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt")) reader new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt")) tokenizer = new StringTokenizer("") tokenizer new StringTokenizer("") static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    } init {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    } reader = new BufferedReader(
                new InputStreamReader(input) ) reader new BufferedReader(
                new InputStreamReader(input) ) tokenizer = new StringTokenizer("") tokenizer new StringTokenizer("") InputStream input input /** get next word */
    static String nextLine() throws IOException{
        return reader.readLine();
    } nextLine {
        return reader.readLine();
    } return reader.readLine(); reader.readLine() reader.readLine reader static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    } next {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    } while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        } ! tokenizer.hasMoreTokens() tokenizer.hasMoreTokens() tokenizer.hasMoreTokens tokenizer {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        } tokenizer = new StringTokenizer(
                    reader.readLine() ) tokenizer new StringTokenizer(
                    reader.readLine() ) return tokenizer.nextToken(); tokenizer.nextToken() tokenizer.nextToken tokenizer static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    } nextInt {
        return Integer.parseInt( next() );
    } return Integer.parseInt( next() ); Integer.parseInt( next() ) Integer.parseInt Integer next() next static long nextLong() throws IOException {
        return Long.parseLong( next() );
    } nextLong {
        return Long.parseLong( next() );
    } return Long.parseLong( next() ); Long.parseLong( next() ) Long.parseLong Long next() next static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    } nextDouble {
        return Double.parseDouble( next() );
    } return Double.parseDouble( next() ); Double.parseDouble( next() ) Double.parseDouble Double next() next public class Main {
	
	static long mod= (long) 1e9+7;
	
		
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	}

} public class Main {
	
	static long mod= (long) 1e9+7;
	
		
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	}

} Main
import java.io.*;
import java.util.*;

public class Main {
	
	static long mod= (long) 1e9+7;
	
		
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		int[] arr=new int[n];
		long ans=0;
		int max=0;
		for(int i=0;i<n;i++) {
			int a=Reader.nextInt();
			if(max>a) {
				ans+=(max-a);
			}
			max=Math.max(a,max);
		}
		out.println(ans);
		
		out.flush();
		
	}

}


class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
        reader = new BufferedReader(
                new FileReader("C:\\Users\\samee\\Desktop\\GRE prep\\confusion.txt"));
        tokenizer = new StringTokenizer("");
    }
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
        return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}