import java.util.Scanner;

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

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

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

	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] A = new long[N];
		for(int i=0;i<N;i++) {
			A[i]=sc.nextInt();
		}
		long check = 0;
		for(int i=1;i<N;i++) {
			if(A[i-1]>A[i]) {
				check += A[i-1]-A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(check);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long[] A = new long[N]; A new long[N] N for(int i=0;i<N;i++) {
			A[i]=sc.nextInt();
		} int i=0; int i=0; i 0 i<N i N i++ i++ i {
			A[i]=sc.nextInt();
		} {
			A[i]=sc.nextInt();
		} A[i]=sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc long check = 0; check 0 for(int i=1;i<N;i++) {
			if(A[i-1]>A[i]) {
				check += 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 {
			if(A[i-1]>A[i]) {
				check += A[i-1]-A[i];
				A[i] = A[i-1];
			}
		} {
			if(A[i-1]>A[i]) {
				check += A[i-1]-A[i];
				A[i] = A[i-1];
			}
		} if(A[i-1]>A[i]) {
				check += 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 {
				check += A[i-1]-A[i];
				A[i] = A[i-1];
			} check += A[i-1]-A[i] check 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(check) System.out.println System.out System System.out check String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] A = new long[N];
		for(int i=0;i<N;i++) {
			A[i]=sc.nextInt();
		}
		long check = 0;
		for(int i=1;i<N;i++) {
			if(A[i-1]>A[i]) {
				check += A[i-1]-A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(check);

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

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

	}
}
