import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	}

} 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();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	}

} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	} main {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[] height = new int[n]; height new int[n] n for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			height[i] = sc.nextInt();
		} {
			height[i] = sc.nextInt();
		} height[i] = sc.nextInt() height[i] height i sc.nextInt() sc.nextInt sc int temp = 0; temp 0 long totalStandHeight = 0; totalStandHeight 0 for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		} {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		} if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			} temp > height[i] temp height[i] height i {
				totalStandHeight += temp - height[i];
			} totalStandHeight += temp - height[i] totalStandHeight temp - height[i] temp height[i] height i if (temp < height[i]) {
				temp = height[i];
			} temp < height[i] temp height[i] height i {
				temp = height[i];
			} temp = height[i] temp height[i] height i System.out.println(totalStandHeight) System.out.println System.out System System.out totalStandHeight sc.close() sc.close sc String[] args args public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	}

} public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}

		int temp = 0;
		long totalStandHeight = 0;
		for (int i = 0; i < n; i++) {

			if (temp > height[i]) {
				totalStandHeight += temp - height[i];
			} else if (temp < height[i]) {
				temp = height[i];
			}

		}

		System.out.println(totalStandHeight);

		sc.close();

	}

}
