
/*
 *   
 *   Date: 07 September 2019
 *   Time: 01:03:00
*/
import java.io.*; 
import java.util.*;


   
 class Main
{   

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long arr[]=new long[n];
        for(int i=0;i<n;i++){
        	arr[i]=sc.nextLong();
        }
        long max=arr[0];
        long diff=0;
        for(int i=1;i<n;i++){
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        }
       
        System.out.println(diff);
     
        
       

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

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long arr[]=new long[n];
        for(int i=0;i<n;i++){
        	arr[i]=sc.nextLong();
        }
        long max=arr[0];
        long diff=0;
        for(int i=1;i<n;i++){
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        }
       
        System.out.println(diff);
     
        
       

       
    } 
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long arr[]=new long[n];
        for(int i=0;i<n;i++){
        	arr[i]=sc.nextLong();
        }
        long max=arr[0];
        long diff=0;
        for(int i=1;i<n;i++){
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        }
       
        System.out.println(diff);
     
        
       

       
    } main {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long arr[]=new long[n];
        for(int i=0;i<n;i++){
        	arr[i]=sc.nextLong();
        }
        long max=arr[0];
        long diff=0;
        for(int i=1;i<n;i++){
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        }
       
        System.out.println(diff);
     
        
       

       
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc long arr[]=new long[n]; arr new long[n] n for(int i=0;i<n;i++){
        	arr[i]=sc.nextLong();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
        	arr[i]=sc.nextLong();
        } {
        	arr[i]=sc.nextLong();
        } arr[i]=sc.nextLong() arr[i] arr i sc.nextLong() sc.nextLong sc long max=arr[0]; max arr[0] arr 0 long diff=0; diff 0 for(int i=1;i<n;i++){
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        } {
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        } if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	} arr[i]>=max arr[i] arr i max {
        		        		max=arr[i];

        		continue;
        	} max=arr[i] max arr[i] arr i continue; {
        		diff+=max-arr[i];
        		arr[i]=max;;
        	} diff+=max-arr[i] diff max-arr[i] max arr[i] arr i arr[i]=max arr[i] arr i max System.out.println(diff) System.out.println System.out System System.out diff String[] args args

/*
 *   
 *   Date: 07 September 2019
 *   Time: 01:03:00
*/
import java.io.*; 
import java.util.*;


   
 class Main
{   

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        long arr[]=new long[n];
        for(int i=0;i<n;i++){
        	arr[i]=sc.nextLong();
        }
        long max=arr[0];
        long diff=0;
        for(int i=1;i<n;i++){
        	if(arr[i]>=max){
        		        		max=arr[i];

        		continue;
        	}else{
        		diff+=max-arr[i];
        		arr[i]=max;;
        	}
        }
       
        System.out.println(diff);
     
        
       

       
    } 
} 
 
 
 
