 
import java.util.Scanner;
 
public class Main{//2 1 5 4 3
    
    public static void solve(int[] arr){
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    }
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    }
} import java.util.Scanner; Scanner java.util public class Main{//2 1 5 4 3
    
    public static void solve(int[] arr){
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    }
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    }
} Main public static void solve(int[] arr){
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    } solve {
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    } long ans = 0; ans 0 int prev = Integer.MIN_VALUE; prev Integer.MIN_VALUE Integer Integer.MIN_VALUE for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        } int i= 0; int i= 0; i 0 i < arr.length i arr.length arr arr.length i++ i++ i {
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        } {
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        } if(arr[i] < prev){
                ans += prev - arr[i];
            } arr[i] < prev arr[i] arr i prev {
                ans += prev - arr[i];
            } ans += prev - arr[i] ans prev - arr[i] prev arr[i] arr i prev = Math.max(prev,arr[i]) prev Math.max(prev,arr[i]) Math.max Math prev arr[i] arr i System.out.println(ans) System.out.println System.out System System.out ans int[] arr arr public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    } main {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    } Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int N=sc.nextInt(); N sc.nextInt() sc.nextInt sc long ans=0; ans 0 int[] arr = new int[N]; arr new int[N] N for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            arr[i]=sc.nextInt();
        } {
            arr[i]=sc.nextInt();
        } arr[i]=sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc solve(arr) solve arr String[] args args public class Main{//2 1 5 4 3
    
    public static void solve(int[] arr){
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    }
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    }
} public class Main{//2 1 5 4 3
    
    public static void solve(int[] arr){
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    }
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    }
} Main
 
import java.util.Scanner;
 
public class Main{//2 1 5 4 3
    
    public static void solve(int[] arr){
     
        long ans = 0;
        int prev = Integer.MIN_VALUE;
        for(int i= 0; i < arr.length; i++){
            if(arr[i] < prev){
                ans += prev - arr[i];
            }
            
           prev = Math.max(prev,arr[i]);
        }
        
        System.out.println(ans);
    }
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        long ans=0;
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();
        }
        
        
        solve(arr);
        
        
    }
}