import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	}
} Main public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	} main {
		Scanner scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) long N = scanner.nextLong(); N scanner.nextLong() scanner.nextLong scanner long A1 = scanner.nextLong(); A1 scanner.nextLong() scanner.nextLong scanner long sum = 0; sum 0 long b = A1; b A1 for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		} long i = 0; long i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		} {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		} long a = scanner.nextLong(); a scanner.nextLong() scanner.nextLong scanner if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			} a < b a b {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} sum += b-a sum b-a b a {
 				b = a;
 				//System.out.println("パターン2");
 			} b = a b a 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 scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		long N = scanner.nextLong();
		long A1 = scanner.nextLong();
		long sum = 0;
		long b = A1;
		//System.out.println("bは" + b);
 		for (long i = 0; i < N-1; i++) {
 			long a = scanner.nextLong();
 			//System.out.println("aは" + a);
 			if (a < b) {
 				sum += b-a;
 				//System.out.println("パターン1");
 			} else {
 				b = a;
 				//System.out.println("パターン2");
 			}
 			//System.out.println(sum);
 		}
 	System.out.println(sum);
	}
}