import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	} main {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan int[] a = new int[n]; a new int[n] n for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt(); int i = 0 ; int i = 0 ; i 0 i < n i n i++ i++ i a[i] = scan.nextInt(); a[i] = scan.nextInt() a[i] a i scan.nextInt() scan.nextInt scan scan.close() scan.close scan long sum = 0; sum 0 int max = a[0]; max a[0] a 0 for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (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) sum+= (max - a[i]);
			else max = a[i];
		} {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		} if(a[i] < max) sum+= (max - a[i]);
			else max = a[i]; a[i] < max a[i] a i max sum+= (max - a[i]) sum (max - a[i]) max a[i] a i max = a[i] max a[i] a i System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	}
} Main
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[] a = new int[n];
		for(int i = 0 ; i < n ; i++) a[i] = scan.nextInt();
		scan.close();
		long sum = 0;
		int max = a[0];
		for(int i = 1 ; i < n ; i++) {
			if(a[i] < max) sum+= (max - a[i]);
			else max = a[i];
		}
		System.out.println(sum);
	}
}
