import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		A[0] = sc.nextInt();
		int a = A[0];
		long s = 0;
		for(int i=1; i<N; i++) {
			A[i] = sc.nextInt();
			if(A[i]<=a) {
				s+=a-A[i];
			}else{
				a = A[i];
			}
		}
		sc.close();
		System.out.println(s);
	}

} 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();
		int[] A = new int[N];
		A[0] = sc.nextInt();
		int a = A[0];
		long s = 0;
		for(int i=1; i<N; i++) {
			A[i] = sc.nextInt();
			if(A[i]<=a) {
				s+=a-A[i];
			}else{
				a = A[i];
			}
		}
		sc.close();
		System.out.println(s);
	}

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

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		A[0] = sc.nextInt();
		int a = A[0];
		long s = 0;
		for(int i=1; i<N; i++) {
			A[i] = sc.nextInt();
			if(A[i]<=a) {
				s+=a-A[i];
			}else{
				a = A[i];
			}
		}
		sc.close();
		System.out.println(s);
	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		A[0] = sc.nextInt();
		int a = A[0];
		long s = 0;
		for(int i=1; i<N; i++) {
			A[i] = sc.nextInt();
			if(A[i]<=a) {
				s+=a-A[i];
			}else{
				a = A[i];
			}
		}
		sc.close();
		System.out.println(s);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] A = new int[N];
		A[0] = sc.nextInt();
		int a = A[0];
		long s = 0;
		for(int i=1; i<N; i++) {
			A[i] = sc.nextInt();
			if(A[i]<=a) {
				s+=a-A[i];
			}else{
				a = A[i];
			}
		}
		sc.close();
		System.out.println(s);
	}

}