import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long tmp1 = 0; tmp1 0 long tmp2 = 0; tmp2 0 long ans = 0; ans 0 for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        } {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        } tmp1 = tmp2 tmp1 tmp2 tmp2 = sc.nextInt() tmp2 sc.nextInt() sc.nextInt sc long x = tmp1 - tmp2; x tmp1 - tmp2 tmp1 tmp2 if (x > 0) {
                ans += x;
                tmp2 += x;
            } x > 0 x 0 {
                ans += x;
                tmp2 += x;
            } ans += x ans x tmp2 += x tmp2 x System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long tmp1 = 0;
        long tmp2 = 0;
        long ans = 0;

        for (int i = 0; i < n; i++) {
            tmp1 = tmp2;
            tmp2 = sc.nextInt();
            long x = tmp1 - tmp2;
            if (x > 0) {
                ans += x;
                tmp2 += x;
            }
        }
        System.out.println(ans);
    }
}
