import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    }
} import java.io.*; io.* java public class Main {
    public static void main(String[] args) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    }
} Main public static void main(String[] args) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    } main {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    } try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        } catch(Exception e){
            System.out.println(e);
        } Exception e {
            System.out.println(e);
        } System.out.println(e) System.out.println System.out System System.out e {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int N = Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String strSplit[] = br.readLine().split(" "); strSplit br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int A[] = new int[N]; A new int[N] N for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            } int i=0; int i=0; i 0 i<N i N i++ i++ i {
                A[i] = Integer.parseInt(strSplit[i]);
            } {
                A[i] = Integer.parseInt(strSplit[i]);
            } A[i] = Integer.parseInt(strSplit[i]) A[i] A i Integer.parseInt(strSplit[i]) Integer.parseInt Integer strSplit[i] strSplit i long ans = 0; ans 0 for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            } int i=0; int i=0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            } {
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            } int a = A[i]; a A[i] A i int b = A[i+1]; b A[i+1] A i+1 i 1 if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                } a > b a b {
                    ans = ans + a - b;
                    A[i+1] = a;
                } ans = ans + a - b ans ans + a - b ans + a ans a b A[i+1] = a A[i+1] A i+1 i 1 a 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) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    }
} Main
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        try{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            
            //String N = br.readLine();
            int N = Integer.parseInt(br.readLine());
            String strSplit[] = br.readLine().split(" ");
            int A[] = new int[N];
            for(int i=0; i<N; i++){
                A[i] = Integer.parseInt(strSplit[i]);
            }
            
            long ans = 0;
            for(int i=0; i<N-1; i++){
                int a = A[i];
                int b = A[i+1];
                
                if(a > b){
                    ans = ans + a - b;
                    A[i+1] = a;
                }
            }
            
            System.out.println(ans);
            
        }catch(Exception e){
            System.out.println(e);
        }
    }
}