import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long ans = 0; ans 0 int p = sc.nextInt(); p sc.nextInt() sc.nextInt sc for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		} {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		} int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc if (p > c) {
				ans += p - c;
				c = p;
			} p > c p c {
				ans += p - c;
				c = p;
			} ans += p - c ans p - c p c c = p c p p = c p c System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long ans = 0;
		int p = sc.nextInt();
		for (int i = 1; i < N; i++) {
			int c = sc.nextInt();
			if (p > c) {
				ans += p - c;
				c = p;
			}
			p = c;
		}
		System.out.println(ans);
	}
}
