import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	}

	private static void solve(long[] hts, int n) {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	}

	private static void solve(long[] hts, int n) {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	} main {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] hts = new long[n]; hts new long[n] n for (int i = 0; i < n; i++) hts[i] = sc.nextLong(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i hts[i] = sc.nextLong(); hts[i] = sc.nextLong() hts[i] hts i sc.nextLong() sc.nextLong sc solve(hts, n) solve hts n String[] args args private static void solve(long[] hts, int n) {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	} solve {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	} long rht = 0 rht 0 lm = hts[0]; lm hts[0] hts 0 for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		} {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		} lm = Math.max(lm, hts[i]) lm Math.max(lm, hts[i]) Math.max Math lm hts[i] hts i if (hts[i] < lm) {
				rht += lm - hts[i];
			} hts[i] < lm hts[i] hts i lm {
				rht += lm - hts[i];
			} rht += lm - hts[i] rht lm - hts[i] lm hts[i] hts i System.out.println(rht) System.out.println System.out System System.out rht long[] hts hts int n n public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	}

	private static void solve(long[] hts, int n) {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	}

	private static void solve(long[] hts, int n) {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] hts = new long[n];
		for (int i = 0; i < n; i++) hts[i] = sc.nextLong();
		solve(hts, n);
	}

	private static void solve(long[] hts, int n) {
		// TODO Auto-generated method stub
		long rht = 0, lm = hts[0];
		for (int i = 1; i < n; i++) {
			lm = Math.max(lm, hts[i]);
			if (hts[i] < lm) {
				rht += lm - hts[i];
			}
		}
		System.out.println(rht);
	}

}
