import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	}
	
} import java.util.*; util.* java public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	}
	
} Main public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	} main {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	} Scanner reader = new Scanner(System.in); reader new Scanner(System.in) int n = reader.nextInt(); n reader.nextInt() reader.nextInt reader long[] arr = new long[n]; arr new long[n] n for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			arr[i] = reader.nextLong();
		} {
			arr[i] = reader.nextLong();
		} arr[i] = reader.nextLong() arr[i] arr i reader.nextLong() reader.nextLong reader long total = 0; total 0 long must = arr[0]; must arr[0] arr 0 for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		} {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		} if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			} arr[i] < must arr[i] arr i must {
				total += must - arr[i];
				arr[i] = must;
			} total += must - arr[i] total must - arr[i] must arr[i] arr i arr[i] = must arr[i] arr i must must = arr[i] must arr[i] arr i System.out.println(total) System.out.println System.out System System.out total reader.close() reader.close reader String[] args args public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	}
	
} public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	}
	
} Main
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		long[] arr = new long[n];
		for (int i = 0; i < n; i++) {
			arr[i] = reader.nextLong();
		}
		long total = 0;
		long must = arr[0];
		for (int i = 1; i < n; i++) {
			if (arr[i] < must) {
				total += must - arr[i];
				arr[i] = must;
			}
			must = arr[i];
		}
		System.out.println(total);
		reader.close();
	}
	
}
