import java.util.*;
import java.io.*;
import java.math.BigInteger;
 
public class Main {
	public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	int N = s.nextInt();
	int[] A = new int[N];
	
	for(int i = 0; i < N; i++){
		A[i] = s.nextInt();
	}

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

    
  }
} import java.util.*; util.* java import java.io.*; io.* java import java.math.BigInteger; BigInteger java.math public class Main {
	public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	int N = s.nextInt();
	int[] A = new int[N];
	
	for(int i = 0; i < N; i++){
		A[i] = s.nextInt();
	}

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

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

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

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

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

    
  } Scanner s = new Scanner(System.in); s new Scanner(System.in) int N = s.nextInt(); N s.nextInt() s.nextInt s int[] A = new int[N]; A new int[N] N for(int i = 0; i < N; i++){
		A[i] = s.nextInt();
	} int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
		A[i] = s.nextInt();
	} {
		A[i] = s.nextInt();
	} A[i] = s.nextInt() A[i] A i s.nextInt() s.nextInt s int max = A[0]; max A[0] A 0 long count = 0; count 0 for(int i = 1; i < N; i++){
		if(max<A[i]){
			max = A[i];
		}else{
			count = count + (max - A[i]);
		}
	} int i = 1; int i = 1; i 1 i < N i N i++ i++ i {
		if(max<A[i]){
			max = A[i];
		}else{
			count = count + (max - A[i]);
		}
	} {
		if(max<A[i]){
			max = A[i];
		}else{
			count = count + (max - A[i]);
		}
	} if(max<A[i]){
			max = A[i];
		}else{
			count = count + (max - A[i]);
		} max<A[i] max A[i] A i {
			max = A[i];
		} max = A[i] max A[i] A i {
			count = count + (max - A[i]);
		} count = count + (max - A[i]) count count + (max - A[i]) count (max - A[i]) max A[i] A i System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main {
	public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	int N = s.nextInt();
	int[] A = new int[N];
	
	for(int i = 0; i < N; i++){
		A[i] = s.nextInt();
	}

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

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

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

    
  }
} Main
import java.util.*;
import java.io.*;
import java.math.BigInteger;
 
public class Main {
	public static void main(String[] args) {
	Scanner s = new Scanner(System.in);
	int N = s.nextInt();
	int[] A = new int[N];
	
	for(int i = 0; i < N; i++){
		A[i] = s.nextInt();
	}

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

    
  }
}