import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        }

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

        System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        }

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        }

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

        System.out.println(ans);
    } main {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        }

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

        System.out.println(ans);
    } Scanner input = new Scanner(System.in); input new Scanner(System.in) int N = input.nextInt(); N input.nextInt() input.nextInt input long[] A = new long[N]; A new long[N] N for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = input.nextLong();
        } {
            A[i] = input.nextLong();
        } A[i] = input.nextLong() A[i] A i input.nextLong() input.nextLong input long maxHeight = A[0]; maxHeight A[0] A 0 long ans = 0; ans 0 for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        } int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        } {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        } if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            } maxHeight > A[i] maxHeight A[i] A i {
                ans += maxHeight - A[i];
            } ans += maxHeight - A[i] ans maxHeight - A[i] maxHeight A[i] A i {
                maxHeight = A[i];
            } maxHeight = A[i] maxHeight A[i] A i 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 input = new Scanner(System.in);
        int N = input.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        }

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

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

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

        System.out.println(ans);
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt();
        long[] A = new long[N];
        for (int i = 0; i < N; i++) {
            A[i] = input.nextLong();
        }

        long maxHeight = A[0];
        long ans = 0;

        for (int i = 1; i < N; i++) {
            if (maxHeight > A[i]) {
                ans += maxHeight - A[i];
            } else {
                maxHeight = A[i];
            }
        }

        System.out.println(ans);
    }
}