import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	}
} import java.io.FileNotFoundException; FileNotFoundException java.io import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) throws FileNotFoundException {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	}
} Main public static void main(String[] args) throws FileNotFoundException {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	} main {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	} 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 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 int height = A[0]; height A[0] A 0 long ans = 0; ans 0 for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		} int i=1; int i=1; i 1 i<N i N i++ i++ i {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		} {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		} if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			} A[i]>height A[i] A i height {
				height = A[i];
			} height = A[i] height A[i] A i {
				ans += height - A[i];
			} ans += height - A[i] ans height - A[i] height 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) throws FileNotFoundException {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	}
} public class Main {
	public static void main(String[] args) throws FileNotFoundException {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	}
} Main
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {

		//    	File file = new File("src/in.txt");
		//    	Scanner sc = new Scanner(file);

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int[] A = new int[N];

		for(int i=0;i<N;i++) {
			A[i] = sc.nextInt();
		}
		int height = A[0];
		long ans = 0;
		for(int i=1;i<N;i++) {
			if(A[i]>height) {
				height = A[i];
			} else {
				ans += height - A[i];
			}
		}
		System.out.println(ans);

	}
}