import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	}
} 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 p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long p = 0; p 0 long ss = 0; ss 0 for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		} {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		} long c = sc.nextLong(); c sc.nextLong() sc.nextLong sc if (c < p) {
				ss += p - c;
				continue;
			} c < p c p {
				ss += p - c;
				continue;
			} ss += p - c ss p - c p c continue; p = c p c System.out.println(ss) System.out.println System.out System System.out ss String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	}
} 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 p = 0;
		long ss = 0;
		for (int i = 0; i < n; i++) {
			long c = sc.nextLong();
			if (c < p) {
				ss += p - c;
				continue;
			}
			p = c;
		}
		System.out.println(ss);
	}
}
