import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc long [] in = new long [a]; in new long [a] a for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            } int i=0; int i=0; i 0 i<a i a i++ i++ i {
                             in[i] = sc.nextLong();
                            } {
                             in[i] = sc.nextLong();
                            } in[i] = sc.nextLong() in[i] in i sc.nextLong() sc.nextLong sc long max = in[0]; max in[0] in 0 long ans = 0; ans 0 for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            } int i=0; int i=0; i 0 i<a i a i++ i++ i {if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            } {if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            } if(max>in[i]){ans+=max-in[i];} max>in[i] max in[i] in i {ans+=max-in[i];} ans+=max-in[i] ans max-in[i] max in[i] in i max=Math.max(in[i],max) max Math.max(in[i],max) Math.max Math in[i] in i max System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	}
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	}
} Main
import java.util.*;
public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		long [] in = new long [a];      
        for(int i=0;i<a;i++){
                             in[i] = sc.nextLong();
                            }		
        long max = in[0];
		long ans = 0;
        for(int i=0;i<a;i++){if(max>in[i]){ans+=max-in[i];}
                             max=Math.max(in[i],max);
                            }       
		System.out.println(ans);
	}
}