import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	}


} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	}


} Main public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	} main {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	} try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		} {
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		} Scanner scan = new Scanner(System.in) Scanner scan = new Scanner(System.in) new Scanner(System.in) int N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long[]A = new long[N]; A new long[N] N for(int i = 0;i<N;i++)A[i] = scan.nextLong(); int i = 0; int i = 0; i 0 i<N i N i++ i++ i A[i] = scan.nextLong(); A[i] = scan.nextLong() A[i] A i scan.nextLong() scan.nextLong scan long ki = A[0]; ki A[0] A 0 long goukei = 0; goukei 0 for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			} int i = 1; int i = 1; i 1 i<N i N i++ i++ i {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			} {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			} if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				} ki<=A[i] ki A[i] A i {
					ki=A[i];
					//System.out.println(0);
				} ki=A[i] ki A[i] A i {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				} goukei+=ki-A[i] goukei ki-A[i] ki A[i] A i System.out.println(goukei) System.out.println System.out System System.out goukei String[]args args public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	}


} public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	}


} Main
import java.util.Scanner;

public class Main {
	public static void main(String[]args) {
		try(Scanner scan = new Scanner(System.in)){
			int N = scan.nextInt();
			long[]A = new long[N];
			for(int i = 0;i<N;i++)A[i] = scan.nextLong();


			long ki = A[0];
			long goukei = 0;
			for(int i = 1;i<N;i++) {
				if(ki<=A[i]) {
					ki=A[i];
					//System.out.println(0);
				}else {
					goukei+=ki-A[i];
					//System.out.println(ki-A[i]);
				}

			}

			System.out.println(goukei);




		}


	}


}
