import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int  n = sc.nextInt();
		
		long arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		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 arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		sc.close();
	}

} Main public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int  n = sc.nextInt();
		
		long arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		sc.close();
	} main {
		
		Scanner sc = new Scanner(System.in);
		
		int  n = sc.nextInt();
		
		long arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int  n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long arr[] = new long[n]; arr new long[n] n for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			arr[i] = sc.nextLong();
		} {
			arr[i] = sc.nextLong();
		} arr[i] = sc.nextLong() arr[i] arr i sc.nextLong() sc.nextLong sc long total = 0; total 0 long largest = arr[0]; largest arr[0] arr 0 for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		} {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		} if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			} arr[i] <= largest arr[i] arr i largest {
				total += largest - arr[i];
			} total += largest - arr[i] total largest - arr[i] largest arr[i] arr i if(arr[i] > largest) {
				largest = arr[i];
			} arr[i] > largest arr[i] arr i largest {
				largest = arr[i];
			} largest = arr[i] largest arr[i] arr i System.out.println(total) System.out.println System.out System System.out total 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 arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		sc.close();
	}

} public class Main {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int  n = sc.nextInt();
		
		long arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		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 arr[] = new long[n];
		
		for(int i=0; i<n; i++) {
			arr[i] = sc.nextLong();
		}
		
		long total = 0;
		long largest = arr[0];
		for(int i=1; i<n; i++) {
			
			if(arr[i] <= largest) {
				total += largest - arr[i];
			}else if(arr[i] > largest) {
				largest = arr[i];
			}
		}
		
		System.out.println(total);
		sc.close();
	}

}
