import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[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.InputStreamReader; InputStreamReader java.io import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[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) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[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 {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[0];
		for (int i = 1; i < n; i++)
			if (a[i] < h)
				ans += h - a[i];
			else
				h = a[i];
		System.out.println(ans);
	} Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); in new Scanner(new BufferedReader(new InputStreamReader(System.in))) int n = in.nextInt(); n in.nextInt() in.nextInt in int[] a = new int[n]; a new int[n] n for (int i = 0; i < n; i++)
			a[i] = in.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i a[i] = in.nextInt(); a[i] = in.nextInt() a[i] a i in.nextInt() in.nextInt in long ans = 0; ans 0 int h = a[0]; h a[0] a 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]; 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) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[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) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[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.InputStreamReader;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
		int n = in.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = in.nextInt();

		long ans = 0;
		int h = a[0];
		for (int i = 1; i < n; i++)
			if (a[i] < h)
				ans += h - a[i];
			else
				h = a[i];
		System.out.println(ans);
	}
}
