import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	}
} Main public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long A[] = new long[N]; A new long[N] N long ans = 0L; ans 0L for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		} int i = 0; int i = 0; i 0 i<N i N i++ i++ i {
			A[i] = sc.nextLong();
		} {
			A[i] = sc.nextLong();
		} A[i] = sc.nextLong() A[i] A i sc.nextLong() sc.nextLong sc for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		} int i = 0; int i = 0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		} {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		} long x =A[i+1] - A[i]; x A[i+1] - A[i] A[i+1] A i+1 i 1 A[i] A i if(x<0) {
				ans -= x;
				A[i+1] -=x;
			} x<0 x 0 {
				ans -= x;
				A[i+1] -=x;
			} ans -= x ans x A[i+1] -=x A[i+1] A i+1 i 1 x System.out.println(ans) System.out.println System.out System System.out ans sc.close() sc.close sc String args[] args public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	}
} public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long A[] = new long[N];

		long ans = 0L;

		for(int i = 0; i<N; i++) {
			A[i] = sc.nextLong();
		}

		for(int i = 0; i<N-1; i++) {
			long x =A[i+1] - A[i];
			if(x<0) {
				ans -= x;
				A[i+1] -=x;
			}
		}

		System.out.println(ans);
		sc.close();
	}
}
