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

import static java.lang.Integer.*;
import static java.lang.Long.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    }

    static long getMinimumTotalHeight(long[] arr, int n) {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    }
} import java.io.*; io.* java import java.util.StringTokenizer; StringTokenizer java.util import static java.lang.Integer.*; import static java.lang.Integer.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Long.*; import static java.lang.Long.*; public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    }

    static long getMinimumTotalHeight(long[] arr, int n) {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    }
} Main public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    } main {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    } BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); read new BufferedReader(new InputStreamReader(System.in)) PrintWriter prnt = new PrintWriter(System.out); prnt new PrintWriter(System.out) int n = parseInt(read.readLine()); n parseInt(read.readLine()) parseInt read.readLine() read.readLine read long[] arr = new long[n]; arr new long[n] n StringTokenizer st = new StringTokenizer(read.readLine()); st new StringTokenizer(read.readLine()) for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = parseLong(st.nextToken());
        } {
            arr[i] = parseLong(st.nextToken());
        } arr[i] = parseLong(st.nextToken()) arr[i] arr i parseLong(st.nextToken()) parseLong st.nextToken() st.nextToken st prnt.println(getMinimumTotalHeight(arr, n)) prnt.println prnt getMinimumTotalHeight(arr, n) getMinimumTotalHeight arr n prnt.close() prnt.close prnt String[] args args static long getMinimumTotalHeight(long[] arr, int n) {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    } getMinimumTotalHeight {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    } long heightOfstool = 0; heightOfstool 0 for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        } {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        } if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            } arr[i - 1] > arr[i] arr[i - 1] arr i - 1 i 1 arr[i] arr i {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            } heightOfstool += (arr[i - 1] - arr[i]) heightOfstool (arr[i - 1] - arr[i]) arr[i - 1] arr i - 1 i 1 arr[i] arr i arr[i] += (arr[i - 1] - arr[i]) arr[i] arr i (arr[i - 1] - arr[i]) arr[i - 1] arr i - 1 i 1 arr[i] arr i return heightOfstool; heightOfstool long[] arr arr int n n public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    }

    static long getMinimumTotalHeight(long[] arr, int n) {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    }
} public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    }

    static long getMinimumTotalHeight(long[] arr, int n) {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    }
} Main
import java.io.*;
import java.util.StringTokenizer;

import static java.lang.Integer.*;
import static java.lang.Long.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter prnt = new PrintWriter(System.out);

        int n = parseInt(read.readLine());
        long[] arr = new long[n];
        StringTokenizer st = new StringTokenizer(read.readLine());
        for (int i = 0; i < n; i++) {
            arr[i] = parseLong(st.nextToken());
        }
        prnt.println(getMinimumTotalHeight(arr, n));
        prnt.close();
    }

    static long getMinimumTotalHeight(long[] arr, int n) {
        long heightOfstool = 0;
        for (int i = 1; i < n; i++) {
            if (arr[i - 1] > arr[i]) {
                heightOfstool += (arr[i - 1] - arr[i]);
                arr[i] += (arr[i - 1] - arr[i]);
            }
        }
        return heightOfstool;
    }
}