import java.util.*;
import java.io.*;

public class Main {
	static FastReader f = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	}
		

	static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
} import java.util.*; util.* java import java.io.*; io.* java public class Main {
	static FastReader f = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	}
		

	static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
} Main static FastReader f = new FastReader(); f new FastReader() static PrintWriter out = new PrintWriter(System.out); out new PrintWriter(System.out) public static void main(String[] args) {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	} main {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	} int n = f.nextInt(); n f.nextInt() f.nextInt f int[] arr = new int[n]; arr new int[n] n for(int i=0;i<n;i++) arr[i] = f.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i arr[i] = f.nextInt(); arr[i] = f.nextInt() arr[i] arr i f.nextInt() f.nextInt f int max = arr[0]; max arr[0] arr 0 long ans = 0; ans 0 for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		} {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		} max = Math.max(max, arr[i]) max Math.max(max, arr[i]) Math.max Math max arr[i] arr i ans += max - arr[i] ans max - arr[i] max arr[i] arr i out.println(ans) out.println out ans out.close() out.close out String[] args args static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	} FastReader BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) StringTokenizer st; st String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		} next {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		} while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			} st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			} try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				} catch(IOException e) {
					e.printStackTrace();
				} IOException e {
					e.printStackTrace();
				} e.printStackTrace() e.printStackTrace e {
					st = new StringTokenizer(br.readLine());
				} st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		} nextLine {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		} String str = ""; str "" try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			} catch(IOException e) {
				e.printStackTrace();
			} IOException e {
				e.printStackTrace();
			} e.printStackTrace() e.printStackTrace e {
				str = br.readLine();
			} str = br.readLine() str br.readLine() br.readLine br return str; str int nextInt() {
			return Integer.parseInt(next());
		} nextInt {
			return Integer.parseInt(next());
		} return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next double nextDouble() {
			return Double.parseDouble(next());
		} nextDouble {
			return Double.parseDouble(next());
		} return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next long nextLong() {
			return Long.parseLong(next());
		} nextLong {
			return Long.parseLong(next());
		} return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public class Main {
	static FastReader f = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	}
		

	static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
} public class Main {
	static FastReader f = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	}
		

	static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
} Main
import java.util.*;
import java.io.*;

public class Main {
	static FastReader f = new FastReader();
	static PrintWriter out = new PrintWriter(System.out);

	public static void main(String[] args) {
		int n = f.nextInt();
		int[] arr = new int[n];
		for(int i=0;i<n;i++) arr[i] = f.nextInt();
		int max = arr[0];
		long ans = 0;
		for(int i=1;i<n;i++) {
			max = Math.max(max, arr[i]);
			ans += max - arr[i];
		}
		out.println(ans);
		out.close();
	}
		

	static class FastReader {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}

		String nextLine() {
			String str = "";

			try {
				str = br.readLine();
			} catch(IOException e) {
				e.printStackTrace();
			}
			return str;
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}

		long nextLong() {
			return Long.parseLong(next());
		}
	}
}