import java.util.*;

public class Main {
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	}
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	}
} Main public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	} main {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	} Scanner keyboard = new Scanner(System.in); keyboard new Scanner(System.in) int N = keyboard.nextInt(); N keyboard.nextInt() keyboard.nextInt keyboard long a1 = keyboard.nextLong(); a1 keyboard.nextLong() keyboard.nextLong keyboard long sum = 0; sum 0 for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		} int n=1; int n=1; n 1 n<N n N n++ n++ n {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		} {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		} long a = keyboard.nextLong(); a keyboard.nextLong() keyboard.nextLong keyboard if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			} a<a1 a a1 {
				sum += a1-a;
			} sum += a1-a sum a1-a a1 a {
				a1 = a;
			} a1 = a a1 a System.out.print(sum) System.out.print System.out System System.out sum keyboard.close() keyboard.close keyboard String[] args args public class Main {
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	}
} public class Main {
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	}
} Main
import java.util.*;

public class Main {
	public static void main(String[] args) {
		
		Scanner keyboard = new Scanner(System.in); 
		
		int N = keyboard.nextInt();
		long a1 = keyboard.nextLong();
		long sum = 0;
		
		for(int n=1; n<N; n++) {
			long a = keyboard.nextLong();
			if(a<a1) {
				sum += a1-a;
			}else {
				a1 = a;
			}
		}
		
		System.out.print(sum);
		keyboard.close();
	}
}