import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	}

} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	}

} Main public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int N = Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br long[] A = new long[N]; A new long[N] N long ans = 0; ans 0 StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken()); int i=0; int i=0; i 0 i<N i N i++ i++ i A[i] = Long.parseLong(st.nextToken()); A[i] = Long.parseLong(st.nextToken()) A[i] A i Long.parseLong(st.nextToken()) Long.parseLong Long st.nextToken() st.nextToken st for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		} int i=1; int i=1; i 1 i<N i N i++ i++ i {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		} {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		} if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			} A[i-1] > A[i] A[i-1] A i-1 i 1 A[i] A i {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			} ans += A[i-1] - A[i] ans A[i-1] - A[i] A[i-1] A i-1 i 1 A[i] A i A[i] = A[i-1] A[i] A i A[i-1] A i-1 i 1 System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	}

} public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	}

} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(br.readLine());
		
		long[] A = new long[N];
		long ans = 0;
		StringTokenizer st = new StringTokenizer(br.readLine());
		for(int i=0; i<N; i++)
			A[i] = Long.parseLong(st.nextToken());
		
		for(int i=1; i<N; i++) {
			if(A[i-1] > A[i]) {
				ans += A[i-1] - A[i];
				A[i] = A[i-1];
			}
		}
		System.out.println(ans);
	}

}