import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		execute20_2();
	}
	
	private static void execute20_2() {
		try (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.nextInt();
			
			long ans=0;
			long max=a[0];
			
			for(int j=1;j<n;j++) {
				if(max>a[j]) {
					ans+=max-a[j];
				} else {
					max=a[j];
				}
			}
			System.out.println(ans);
		}
	}
} import java.util.ArrayDeque; ArrayDeque java.util import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.Comparator; Comparator java.util import java.util.Deque; Deque java.util import java.util.HashMap; HashMap java.util import java.util.HashSet; HashSet java.util import java.util.List; List java.util import java.util.Map; Map java.util import java.util.Queue; Queue java.util import java.util.Scanner; Scanner java.util import java.util.Set; Set java.util public class Main {

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

	public static void main(String[] args) {
		execute20_2();
	}
	
	private static void execute20_2() {
		try (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.nextInt();
			
			long ans=0;
			long max=a[0];
			
			for(int j=1;j<n;j++) {
				if(max>a[j]) {
					ans+=max-a[j];
				} else {
					max=a[j];
				}
			}
			System.out.println(ans);
		}
	}
} public class Main {

	public static void main(String[] args) {
		execute20_2();
	}
	
	private static void execute20_2() {
		try (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.nextInt();
			
			long ans=0;
			long max=a[0];
			
			for(int j=1;j<n;j++) {
				if(max>a[j]) {
					ans+=max-a[j];
				} else {
					max=a[j];
				}
			}
			System.out.println(ans);
		}
	}
} Main
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		execute20_2();
	}
	
	private static void execute20_2() {
		try (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.nextInt();
			
			long ans=0;
			long max=a[0];
			
			for(int j=1;j<n;j++) {
				if(max>a[j]) {
					ans+=max-a[j];
				} else {
					max=a[j];
				}
			}
			System.out.println(ans);
		}
	}
}