import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	}
} Main public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	} main {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	} BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); in new BufferedReader(new InputStreamReader(System.in)) int n = Integer.parseInt(in.readLine()); n Integer.parseInt(in.readLine()) Integer.parseInt Integer in.readLine() in.readLine in String[] tokens = in.readLine().split(" "); tokens in.readLine().split(" ") in.readLine().split in.readLine() in.readLine in " " int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = Integer.parseInt(tokens[i]); a[i] = Integer.parseInt(tokens[i]) a[i] a i Integer.parseInt(tokens[i]) Integer.parseInt Integer tokens[i] tokens i int h = a[0]; h a[0] a 0 long ans = 0; ans 0 for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		} {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		} if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i]; a[i] <= h a[i] a i h ans += h-a[i] ans h-a[i] h a[i] a i h = a[i] h a[i] a i 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) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	}
} public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	}
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(in.readLine());
		String[] tokens = in.readLine().split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = Integer.parseInt(tokens[i]);
		int h = a[0];
		long ans = 0;
		for (int i = 1; i < n; i++) {
			if (a[i] <= h)
				ans += h-a[i];
			else
				h = a[i];
		}
		System.out.println(ans);
	}
}
