import java.util.Scanner;

public class Main {

	static long solve(int n, int[] a) {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	}

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {

	static long solve(int n, int[] a) {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	}

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	}
} Main static long solve(int n, int[] a) {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	} solve {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	} long sum = 0; sum 0 for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		} {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		} if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			} a[i] < a[i - 1] a[i] a i a[i - 1] a i - 1 i 1 {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			} int num = a[i - 1] - a[i]; num a[i - 1] - a[i] a[i - 1] a i - 1 i 1 a[i] a i sum += num sum num a[i] += num a[i] a i num return sum; sum int n n int[] a a public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	} main {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	} Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) int n = stdIn.nextInt(); n stdIn.nextInt() stdIn.nextInt stdIn int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			a[i] = stdIn.nextInt();
		} {
			a[i] = stdIn.nextInt();
		} a[i] = stdIn.nextInt() a[i] a i stdIn.nextInt() stdIn.nextInt stdIn long result = solve(n, a); result solve(n, a) solve n a System.out.println(result) System.out.println System.out System System.out result stdIn.close() stdIn.close stdIn String[] args args public class Main {

	static long solve(int n, int[] a) {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	}

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	}
} public class Main {

	static long solve(int n, int[] a) {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	}

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	}
} Main
import java.util.Scanner;

public class Main {

	static long solve(int n, int[] a) {
		long sum = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] < a[i - 1]) {
				int num = a[i - 1] - a[i];
				sum += num;
				a[i] += num;
			}
		}

		return sum;
	}

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);

		int n = stdIn.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = stdIn.nextInt();
		}
		long result = solve(n, a);
		System.out.println(result);

		stdIn.close();
	}
}
