import java.util.Scanner;

public class Main {
    public long getTotalHeightOfStools(int n, int[] arr) {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    }

    public static void main(String[] args) throws Exception {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public long getTotalHeightOfStools(int n, int[] arr) {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    }

    public static void main(String[] args) throws Exception {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    }
} Main public long getTotalHeightOfStools(int n, int[] arr) {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    } getTotalHeightOfStools {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    } long total = 0; total 0 int prevHeight = arr[0]; prevHeight arr[0] arr 0 for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        } {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        } if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            } prevHeight > arr[i] prevHeight arr[i] arr i {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            } total += prevHeight - arr[i] total prevHeight - arr[i] prevHeight arr[i] arr i arr[i] = prevHeight arr[i] arr i prevHeight prevHeight = arr[i] prevHeight arr[i] arr i return total; total int n n int[] arr arr public static void main(String[] args) throws Exception {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    } main {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    } Main solution = new Main(); solution new Main() Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner int[] arr = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = scanner.nextInt();
        } {
            arr[i] = scanner.nextInt();
        } arr[i] = scanner.nextInt() arr[i] arr i scanner.nextInt() scanner.nextInt scanner long answer = solution.getTotalHeightOfStools(n, arr); answer solution.getTotalHeightOfStools(n, arr) solution.getTotalHeightOfStools solution n arr System.out.println(answer) System.out.println System.out System System.out answer String[] args args public class Main {
    public long getTotalHeightOfStools(int n, int[] arr) {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    }

    public static void main(String[] args) throws Exception {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    }
} public class Main {
    public long getTotalHeightOfStools(int n, int[] arr) {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    }

    public static void main(String[] args) throws Exception {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    }
} Main
import java.util.Scanner;

public class Main {
    public long getTotalHeightOfStools(int n, int[] arr) {
        long total = 0;
        int prevHeight = arr[0];
        for (int i = 1; i < n; i++) {
            if (prevHeight > arr[i]) {
                total += prevHeight - arr[i];
                arr[i] = prevHeight;
            }

            prevHeight = arr[i];
        }
        return total;
    }

    public static void main(String[] args) throws Exception {
        Main solution = new Main();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        long answer = solution.getTotalHeightOfStools(n, arr);
        System.out.println(answer);
    }
}