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