import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	}

} import java.util.ArrayList; ArrayList java.util import java.util.Iterator; Iterator java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	}

} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	} main {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long n = sc.nextLong(); n sc.nextLong() sc.nextLong sc long result = 0; result 0 long num = sc.nextLong(); num sc.nextLong() sc.nextLong sc for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		} {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		} long a = sc.nextInt(); a sc.nextInt() sc.nextInt sc if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			} a >= num a num {
				num = a;
			} num = a num a {
				result = result + num - a;
			} result = result + num - a result result + num - a result + num result num a System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	}

} public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	}

} Main
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();

		long result = 0;
		long num = sc.nextLong();

		for (int i = 1; i < n; i++) {
			long a = sc.nextInt();
			if (a >= num) {
				num = a;
			} else {
				result = result + num - a;
			}
		}

		System.out.println(result);

	}

}
