import java.util.*; 
import java.lang.*; 


public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        }
} import java.util.*; util.* java import java.lang.*; lang.* java public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        }
} Main public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        } main {
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long[] A = new long[N]; A new long[N] N for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                } int i=0; int i=0; i 0 i<N i N i++ i++ i {
                    A[i] = scan.nextLong();
                } {
                    A[i] = scan.nextLong();
                } A[i] = scan.nextLong() A[i] A i scan.nextLong() scan.nextLong scan long sum = 0; sum 0 if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                } N==1 N 1 {
                    System.out.println(0);
                } System.out.println(0) System.out.println System.out System System.out 0 {
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                } for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    } int i=1; int i=1; i 1 i<N i N i++ i++ i {
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    } {
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    } if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        } A[i-1]>=A[i] A[i-1] A i-1 i 1 A[i] A i {
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        } sum += A[i-1] -A[i] sum A[i-1] -A[i] A[i-1] A i-1 i 1 A[i] A i A[i] = A[i-1] A[i] A i A[i-1] A i-1 i 1 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        }
} public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        }
} Main
import java.util.*; 
import java.lang.*; 


public class Main{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
                
                int N = scan.nextInt();
                long[] A = new long[N];
                
                for(int i=0; i<N;i++){
                    A[i] = scan.nextLong();
                }
                
                long sum = 0;
                
                if(N==1){
                    System.out.println(0);
                }else{
                    for(int i=1; i<N;i++){
                        if(A[i-1]>=A[i]){
                            sum += A[i-1] -A[i];
                            A[i] = A[i-1];                            
                        }
                    }
                     System.out.println(sum);
                }                          
        }
}