//Coded By Visnunathan

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.lang.*;

public class Main {
    static int[] arr;
    static int n,k;
    public static void main(String[] args) {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    }

    static final Random random=new Random();
    
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

    
} import java.io.*; io.* java import java.util.*; util.* java import java.text.*; text.* java import java.math.*; math.* java import java.lang.*; lang.* java public class Main {
    static int[] arr;
    static int n,k;
    public static void main(String[] args) {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    }

    static final Random random=new Random();
    
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

    
} Main static int[] arr; arr static int n n k; k static final Random random=new Random(); random new Random() public static void main(String[] args) {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    } main {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    } FastScanner sc=new FastScanner(); sc new FastScanner() n = sc.nextInt() n sc.nextInt() sc.nextInt sc arr = sc.readArray(n) arr sc.readArray(n) sc.readArray sc n long ans = 0; ans 0 int max = arr[0]; max arr[0] arr 0 for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            } int i=1; int i=1; i 1 i<n i n i++ i++ i {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            } {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            } if(max > arr[i]) ans+=(max-arr[i]); max > arr[i] max arr[i] arr i ans+=(max-arr[i]) ans (max-arr[i]) max arr[i] arr i max = Math.max(max,arr[i]) max Math.max(max,arr[i]) Math.max Math max arr[i] arr i System.out.println(ans) System.out.println System.out System System.out ans String[] args args static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    } ruffleSort {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    } int n=a.length; n a.length a a.length for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        } {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        } int oi=random.nextInt(n) oi random.nextInt(n) random.nextInt random n temp=a[oi]; temp a[oi] a oi a[oi]=a[i] a[oi] a oi a[i] a i a[i]=temp a[i] a i temp Arrays.sort(a) Arrays.sort Arrays a int[] a a static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    } FastScanner BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st=new StringTokenizer(""); st new StringTokenizer("") String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        } next {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        } while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    st=new StringTokenizer(br.readLine());
                } st=new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        } readArray {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        } int[] a=new int[n]; a new int[n] n for (int i=0; i<n; i++) a[i]=nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=nextInt(); a[i]=nextInt() a[i] a i nextInt() nextInt return a; a int n n long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main {
    static int[] arr;
    static int n,k;
    public static void main(String[] args) {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    }

    static final Random random=new Random();
    
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

    
} public class Main {
    static int[] arr;
    static int n,k;
    public static void main(String[] args) {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    }

    static final Random random=new Random();
    
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

    
} Main
//Coded By Visnunathan

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.lang.*;

public class Main {
    static int[] arr;
    static int n,k;
    public static void main(String[] args) {
        FastScanner sc=new FastScanner();
        // int T=sc.nextInt();
        // for (int tt=0; tt<T; tt++) {
            n = sc.nextInt();
            arr = sc.readArray(n);
            long ans = 0;
            int max = arr[0];
            for(int i=1; i<n; i++) {
                if(max > arr[i]) ans+=(max-arr[i]);
                max = Math.max(max,arr[i]);
            }
            System.out.println(ans);
        //}
    }

    static final Random random=new Random();
    
    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort 
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }
    
    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }
        
        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

    
}
