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

public class Main {
    private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    private static StringTokenizer st;

    private static int readInt() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }

    private static int[] readIntArray(final int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    }

    public static void main(String[] args) throws IOException {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    }

    private static long solve(int[] a) {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    }
} import java.io.*; io.* java import java.util.Arrays; Arrays java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main {
    private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    private static StringTokenizer st;

    private static int readInt() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }

    private static int[] readIntArray(final int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    }

    public static void main(String[] args) throws IOException {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    }

    private static long solve(int[] a) {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    }
} Main private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); pw new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))) private static StringTokenizer st; st private static int readInt() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    } readInt {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    } while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return Integer.parseInt(st.nextToken()); Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st private static int[] readIntArray(final int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    } readIntArray {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    } int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) a[i] = readInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = readInt(); a[i] = readInt() a[i] a i readInt() readInt return a; a final int n n public static void main(String[] args) throws IOException {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    } main {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    } pw.println(solve(readIntArray(readInt()))) pw.println pw solve(readIntArray(readInt())) solve readIntArray(readInt()) readIntArray readInt() readInt pw.close() pw.close pw String[] args args private static long solve(int[] a) {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    } solve {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    } int n = a.length; n a.length a a.length long res = 0; res 0 int max = a[0]; max a[0] a 0 for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        } {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        } if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            } a[i] < max a[i] a i max {
                res += max - a[i];
            } res += max - a[i] res max - a[i] max a[i] a i {
                max = a[i];
            } max = a[i] max a[i] a i return res; res int[] a a public class Main {
    private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    private static StringTokenizer st;

    private static int readInt() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }

    private static int[] readIntArray(final int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    }

    public static void main(String[] args) throws IOException {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    }

    private static long solve(int[] a) {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    }
} public class Main {
    private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    private static StringTokenizer st;

    private static int readInt() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }

    private static int[] readIntArray(final int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    }

    public static void main(String[] args) throws IOException {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    }

    private static long solve(int[] a) {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    }
} Main
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    private static StringTokenizer st;

    private static int readInt() throws IOException {
        while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
        return Integer.parseInt(st.nextToken());
    }

    private static int[] readIntArray(final int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) a[i] = readInt();
        return a;
    }

    public static void main(String[] args) throws IOException {
        pw.println(solve(readIntArray(readInt())));
        pw.close();
    }

    private static long solve(int[] a) {
        int n = a.length;

        long res = 0;
        int max = a[0];
        for (int i = 1; i < n; i++) {
            if (a[i] < max) {
                res += max - a[i];
            } else {
                max = a[i];
            }
        }

        return res;
    }
}