import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.StringTokenizer;

public class Main {
    private static void solve() throws Exception {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    }

    private static FastScanner fs = new FastScanner();
    private static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    }

    static void debug(Object... O) {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    }

    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());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextString() {
            return next();
        }
    }
} import java.io.*; io.* java import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.BitSet; BitSet java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    private static void solve() throws Exception {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    }

    private static FastScanner fs = new FastScanner();
    private static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    }

    static void debug(Object... O) {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    }

    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());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextString() {
            return next();
        }
    }
} Main private static FastScanner fs = new FastScanner(); fs new FastScanner() private static PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) private static void solve() throws Exception {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    } solve {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    } int n = fs.nextInt(); n fs.nextInt() fs.nextInt fs int[] A = fs.readArray(n); A fs.readArray(n) fs.readArray fs n int cur = A[0]; cur A[0] A 0 long ans = 0; ans 0 for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       } int i = 1; int i = 1; i 1 i < n i n i ++ i ++ i {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       } {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       } ans += Math.max(cur-A[i],0) ans Math.max(cur-A[i],0) Math.max Math cur-A[i] cur A[i] A i 0 cur = Math.max(cur, A[i]) cur Math.max(cur, A[i]) Math.max Math cur A[i] A i out.println(ans) out.println out ans public static void main(String[] args) throws Exception {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    } main {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    } int T = 1; T 1 for (int t = 0; t < T; t++) {
            solve();
        } int t = 0; int t = 0; t 0 t < T t T t++ t++ t {
            solve();
        } {
            solve();
        } solve() solve out.close() out.close out String[] args args static void debug(Object... O) {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    } debug {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    } System.err.print("DEBUG ") System.err.print System.err System System.err "DEBUG " System.err.println(Arrays.deepToString(O)) System.err.println System.err System System.err Arrays.deepToString(O) Arrays.deepToString Arrays O Object... O O 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());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextString() {
            return 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 double nextDouble() {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextString() {
            return next();
        } nextString {
            return next();
        } return next(); next() next public class Main {
    private static void solve() throws Exception {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    }

    private static FastScanner fs = new FastScanner();
    private static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    }

    static void debug(Object... O) {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    }

    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());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextString() {
            return next();
        }
    }
} public class Main {
    private static void solve() throws Exception {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    }

    private static FastScanner fs = new FastScanner();
    private static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    }

    static void debug(Object... O) {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    }

    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());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextString() {
            return next();
        }
    }
} Main
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.StringTokenizer;

public class Main {
    private static void solve() throws Exception {
       int n = fs.nextInt();
       int[] A = fs.readArray(n);
       int cur = A[0];
       long ans = 0;
       for (int i = 1; i < n; i ++ ) {
           ans += Math.max(cur-A[i],0);
           cur = Math.max(cur, A[i]);
       }
       out.println(ans);
    }

    private static FastScanner fs = new FastScanner();
    private static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws Exception {
        int T = 1;
        for (int t = 0; t < T; t++) {
            solve();
        }
        out.close();
    }

    static void debug(Object... O) {
        System.err.print("DEBUG ");
        System.err.println(Arrays.deepToString(O));
    }

    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());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextString() {
            return next();
        }
    }
}
