import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    }
} Main public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    } main {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int N = s.nextInt(); N s.nextInt() s.nextInt s long[] A = new long[N]; A new long[N] N long count = 0; count 0 A[0] = s.nextLong() A[0] A 0 s.nextLong() s.nextLong s for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        } {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        } A[i] = s.nextLong() A[i] A i s.nextLong() s.nextLong s if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            } A[i - 1] > A[i] A[i - 1] A i - 1 i 1 A[i] A i {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            } count += (A[i - 1] - A[i]) count (A[i - 1] - A[i]) A[i - 1] A i - 1 i 1 A[i] A i A[i] = A[i - 1] A[i] A i A[i - 1] A i - 1 i 1 System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int N = s.nextInt();
        long[] A = new long[N];
        long count = 0;

        A[0] = s.nextLong();
        for (int i = 1; i < N; i++) {
            A[i] = s.nextLong();
            if (A[i - 1] > A[i]) {
                count += (A[i - 1] - A[i]);
                A[i] = A[i - 1];
            }
        }

        System.out.println(count);
    }
}