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];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	}
} 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];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	}
} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	} main {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	} 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 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 long stool = 0; stool 0 int max = a[0]; max a[0] a 0 for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			max = Math.max(a[i], max);
			stool += max - a[i];
		} {
			max = Math.max(a[i], max);
			stool += max - a[i];
		} max = Math.max(a[i], max) max Math.max(a[i], max) Math.max Math a[i] a i max stool += max - a[i] stool max - a[i] max a[i] a i System.out.println(stool) System.out.println System.out System System.out stool in.close() in.close in 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];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	}
} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	}
} Main
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];
		for (int i = 0; i < n; i++) {
			a[i] = in.nextInt();
		}
		
		long stool = 0;
		int max = a[0];
		for (int i = 0; i < n; i++) {
			max = Math.max(a[i], max);
			stool += max - a[i];
		}
		
		System.out.println(stool);
		
		in.close();
	}
}
