import java.math.BigInteger;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	}
} import java.math.BigInteger; BigInteger java.math import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	}
} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	} main {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int N = in.nextInt(); N in.nextInt() in.nextInt in int[] A = new int[N]; A new int[N] N BigInteger a = new BigInteger("0"); a new BigInteger("0") int b = 0; b 0 for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		} int i=0; int i=0; i 0 i<N i N i++ i++ i {
			A[i] = in.nextInt();
		} {
			A[i] = in.nextInt();
		} A[i] = in.nextInt() A[i] A i in.nextInt() in.nextInt in for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		} int i=0; int i=0; i 0 i<N i N i++ i++ i {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		} {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		} if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			} !(i == 0) && A[i-1] > A[i] !(i == 0) (i == 0) i 0 A[i-1] > A[i] A[i-1] A i-1 i 1 A[i] A i {
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			} b = A[i-1] - A[i] b A[i-1] - A[i] A[i-1] A i-1 i 1 A[i] A i A[i] += b A[i] A i b a = a.add(BigInteger.valueOf(b)) a a.add(BigInteger.valueOf(b)) a.add a BigInteger.valueOf(b) BigInteger.valueOf BigInteger b b = 0 b 0 System.out.println(a) System.out.println System.out System System.out a String[] args args public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	}
} public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	}
} Main
import java.math.BigInteger;
import java.util.Scanner;
public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		int[] A = new int[N];
		BigInteger a = new BigInteger("0");
		int b = 0;
		
		for(int i=0; i<N; i++) {
			A[i] = in.nextInt();
		}
		
		for(int i=0; i<N; i++) {
			if(!(i == 0) && A[i-1] > A[i]){
				b = A[i-1] - A[i];
				A[i] += b;
				a = a.add(BigInteger.valueOf(b));
				b = 0;
			}
		}
		
		System.out.println(a);
	}
}
