import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	}

} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	}

} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	} main {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) String N = scan.nextLine(); N scan.nextLine() scan.nextLine scan int n = Integer.parseInt(N); n Integer.parseInt(N) Integer.parseInt Integer N String L = scan.nextLine(); L scan.nextLine() scan.nextLine scan String[] l = L.split(" "); l L.split(" ") L.split L " " long[] a = new long[n]; a new long[n] n for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		} int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i {
			a[i] = Long.parseLong(l[i]);
		} {
			a[i] = Long.parseLong(l[i]);
		} a[i] = Long.parseLong(l[i]) a[i] a i Long.parseLong(l[i]) Long.parseLong Long l[i] l i long max = a[0]; max a[0] a 0 long total = 0; total 0 for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		} int i = 1 ; int i = 1 ; i 1 i < n i n i++ i++ i {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		} {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		} if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			} a[i] < max a[i] a i max {
				total += max - a[i];
			} total += max - a[i] total max - a[i] max a[i] a i {
				max = a[i];
			} max = a[i] max a[i] a i System.out.println(total) System.out.println System.out System System.out total scan.close() scan.close scan String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	}

} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	}

} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		String N = scan.nextLine();
		int n = Integer.parseInt(N);

		String L = scan.nextLine();
		String[] l = L.split(" ");
		long[] a = new long[n];
		for(int i = 0 ; i < n ; i++) {
			a[i] = Long.parseLong(l[i]);
		}

		long max = a[0];
		long total = 0;
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) {
				total += max - a[i];
			}else {
				max = a[i];
			}
		}

		System.out.println(total);

		scan.close();


	}

}
