import java.util.*;
import java.io.*;
public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	
	
	//Main
	void main() {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	}
    
    
		
} import java.util.*; util.* java import java.io.*; io.* java public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	
	
	//Main
	void main() {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	}
    
    
		
} Main static Scanner inp= new Scanner(System.in); inp new Scanner(System.in) static PrintWriter out= new PrintWriter(System.out); out new PrintWriter(System.out) public static void main(String args[]) {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	} main {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	} Main g=new Main(); g new Main() g.main() g.main g out.close() out.close out String args[] args //Main
	void main() {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	} main {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	} int n=inp.nextInt(); n inp.nextInt() inp.nextInt inp long a[]=new long[n]; a new long[n] n for(int i=0;i<n;i++) a[i]=inp.nextLong(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=inp.nextLong(); a[i]=inp.nextLong() a[i] a i inp.nextLong() inp.nextLong inp long gg=0; gg 0 long max=a[0]; max a[0] a 0 for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		} {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		} if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			} a[i]<max a[i] a i max {
				gg+=max-a[i];
			} gg+=max-a[i] gg max-a[i] max a[i] a i {
				max=a[i];
			} max=a[i] max a[i] a i System.out.println(gg) System.out.println System.out System System.out gg public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	
	
	//Main
	void main() {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	}
    
    
		
} public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	
	
	//Main
	void main() {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	}
    
    
		
} Main
import java.util.*;
import java.io.*;
public class Main {

	static Scanner inp= new Scanner(System.in);
	static PrintWriter out= new PrintWriter(System.out);
	public static void main(String args[]) {		    	
	   	Main g=new Main();
	   	g.main();
	   	out.close();
	}

	
	
	//Main
	void main() {
		
		int n=inp.nextInt();
		long a[]=new long[n];
		for(int i=0;i<n;i++) a[i]=inp.nextLong();
		long gg=0;
		long max=a[0];
		for(int i=1;i<n;i++) {
			if(a[i]<max) {
				gg+=max-a[i];
			}else {
				max=a[i];
			}
		}
		System.out.println(gg);
	}
    
    
		
}
