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

public class Main {
    static StringBuilder sb;

    public static void solve(int n, int[] arr) {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
    static StringBuilder sb;

    public static void solve(int n, int[] arr) {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    }
} Main static StringBuilder sb; sb public static void solve(int n, int[] arr) {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    } solve {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    } int highest = 0; highest 0 long sum = 0; sum 0 for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        } {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        } if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i]; arr[i] < highest arr[i] arr i highest sum += highest - arr[i] sum highest - arr[i] highest arr[i] arr i highest = arr[i] highest arr[i] arr i sb.append(sum) sb.append sb sum int n n int[] arr arr public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    } main {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) PrintWriter pw = new PrintWriter(System.out); pw new PrintWriter(System.out) sb = new StringBuilder() sb new StringBuilder() int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] arr = new int[n]; arr new int[n] n for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc solve(n, arr) solve n arr pw.print(sb) pw.print pw sb pw.flush() pw.flush pw sc.close() sc.close sc String[] args args public class Main {
    static StringBuilder sb;

    public static void solve(int n, int[] arr) {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    }
} public class Main {
    static StringBuilder sb;

    public static void solve(int n, int[] arr) {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    }
} Main
import java.util.*;
import java.io.*;

public class Main {
    static StringBuilder sb;

    public static void solve(int n, int[] arr) {
        int highest = 0;
        long sum = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] < highest) sum += highest - arr[i];
            else highest = arr[i];
        }
        sb.append(sum);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);
        sb = new StringBuilder();

        int n = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        solve(n, arr);

        pw.print(sb);
        pw.flush();
        sc.close();
    }
}