import java.util.Scanner;

public class Main {

	public static void main(final 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	}
	public static <T> void prtln(T t) { System.out.println(t); }
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(final 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	}
	public static <T> void prtln(T t) { System.out.println(t); }
} Main public static void main(final 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	} 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	} 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.nextLong();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = sc.nextLong();
		} {
			a[i] = sc.nextLong();
		} a[i] = sc.nextLong() a[i] a i sc.nextLong() sc.nextLong sc sc.close() sc.close sc long ret = 0L; ret 0L long kijun = a[0]; kijun a[0] a 0 for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		} {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		} if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			} kijun > a[i] kijun a[i] a i {
				ret += kijun - a[i];
			} ret += kijun - a[i] ret kijun - a[i] kijun a[i] a i {
				kijun = a[i];
			} kijun = a[i] kijun a[i] a i prtln(ret) prtln ret final String[] args args public static <T> void prtln(T t) { System.out.println(t); } prtln { System.out.println(t); } System.out.println(t) System.out.println System.out System System.out t T t t public class Main {

	public static void main(final 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	}
	public static <T> void prtln(T t) { System.out.println(t); }
} public class Main {

	public static void main(final 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	}
	public static <T> void prtln(T t) { System.out.println(t); }
} Main
import java.util.Scanner;

public class Main {

	public static void main(final 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.nextLong();
		}
		sc.close();
		long ret = 0L;
		long kijun = a[0];
		for(int i = 1; i < n; i++) {
			if(kijun > a[i]) {
				ret += kijun - a[i];
			} else {
				kijun = a[i];
			}
		}
		prtln(ret);
	}
	public static <T> void prtln(T t) { System.out.println(t); }
}
