import java.util.Scanner;
public class Main {
	public static int N;
	public static int[] A;
	public static int[] A_work;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	}

	private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static int N;
	public static int[] A;
	public static int[] A_work;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	}

	private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	}
} Main public static int N; N public static int[] A; A public static int[] A_work; A_work public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	} main {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) N = Integer.parseInt(scan.next()) N Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan A = new int[N] A new int[N] N A_work = new int[N] A_work new int[N] N for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		} int n = 0; int n = 0; n 0 n < N n N n++ n++ n {
			A[n] = Integer.parseInt(scan.next());
		} {
			A[n] = Integer.parseInt(scan.next());
		} A[n] = Integer.parseInt(scan.next()) A[n] A n Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan scan.close() scan.close scan System.out.println(search()) System.out.println System.out System System.out search() search String[] args args private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	} search {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	} long sum = 0; sum 0 for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		} int n = 0; int n = 0; n 0 n < N-1 n N-1 N 1 n++ n++ n {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		} {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		} int X = A[n] + A_work[n]; X A[n] + A_work[n] A[n] A n A_work[n] A_work n int Y = A[n + 1] + A_work[n + 1]; Y A[n + 1] + A_work[n + 1] A[n + 1] A n + 1 n 1 A_work[n + 1] A_work n + 1 n 1 if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			} X > Y X Y {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			} int diff = X - Y; diff X - Y X Y A_work[n + 1]+=diff A_work[n + 1] A_work n + 1 n 1 diff Y+=diff Y diff sum+=diff sum diff return sum; sum public class Main {
	public static int N;
	public static int[] A;
	public static int[] A_work;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	}

	private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	}
} public class Main {
	public static int N;
	public static int[] A;
	public static int[] A_work;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	}

	private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	}
} Main
import java.util.Scanner;
public class Main {
	public static int N;
	public static int[] A;
	public static int[] A_work;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = Integer.parseInt(scan.next());
		A = new int[N];
		A_work = new int[N];
		for (int n = 0;n < N ;n++ ) {
			A[n] = Integer.parseInt(scan.next());
		}
		scan.close();

		System.out.println(search());
	}

	private static long search() {
		long sum = 0;
		for (int n = 0;n < N-1 ;n++ ) {
			int X = A[n] + A_work[n];
			int Y = A[n + 1] + A_work[n + 1];
			if (X > Y) {
				int diff = X - Y;
				A_work[n + 1]+=diff;
				Y+=diff;
				sum+=diff;
			}
		}
		return sum;
	}
}
