import java.util.*;

public class Main {
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	}

} import java.util.*; util.* java public class Main {
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	}

} Main public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	} main {

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc Long[] Ai = new Long[N]; Ai new Long[N] N Long sum = (long) 0; sum (long) 0 for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		} int i = 0; int i = 0; i 0 i<N i N i++ i++ i {
			Ai[i] = sc.nextLong();
		} {
			Ai[i] = sc.nextLong();
		} Ai[i] = sc.nextLong() Ai[i] Ai i sc.nextLong() sc.nextLong sc Long max = (long) 0; max (long) 0 for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		} int i = 0; int i = 0; i 0 i<N-1 i N-1 N 1 i++ i++ i {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		} {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		} long a1 = Ai[i]; a1 Ai[i] Ai i long a2 = Ai[i+1]; a2 Ai[i+1] Ai i+1 i 1 if(a1>max){
				max=a1;
			} a1>max a1 max {
				max=a1;
			} max=a1 max a1 if(max>a2){
				sum += (max-a2);
              	
			} max>a2 max a2 {
				sum += (max-a2);
              	
			} sum += (max-a2) sum (max-a2) max a2 System.out.println(sum) System.out.println System.out System System.out sum return; String[] args args public class Main {
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	}

} public class Main {
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	}

} Main
import java.util.*;

public class Main {
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		
		Long[] Ai = new Long[N];
		Long sum = (long) 0;
		
		for(int i = 0; i<N; i++) {
			Ai[i] = sc.nextLong();
		}
	  
		Long max = (long) 0;

		for(int i = 0; i<N-1; i++) {
			long a1 = Ai[i];
			long a2 = Ai[i+1];
          
          	if(a1>max){
				max=a1;
			}
          
			if(max>a2){
				sum += (max-a2);
              	
			}
		}

		System.out.println(sum);	
		return;
	}

}
