import java.util.Scanner;

public class Main {
	static int N=0;
	static long count=0L;
	static long max=0L;
	public static void main(String args[]) {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	static int N=0;
	static long count=0L;
	static long max=0L;
	public static void main(String args[]) {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	}
} Main static int N=0; N 0 static long count=0L; count 0L static long max=0L; max 0L public static void main(String args[]) {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	} main {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	} try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		} {
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		} Scanner sc = new Scanner(System.in) Scanner sc = new Scanner(System.in) new Scanner(System.in) N = Integer.parseInt(sc.next()) N Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc Long a[] = new Long[N]; a new Long[N] N for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			} int i=0; int i=0; i 0 i<N i N i++ i++ i {
				a[i] = Long.parseLong(sc.next());
			} {
				a[i] = Long.parseLong(sc.next());
			} a[i] = Long.parseLong(sc.next()) a[i] a i Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			} int i=0; int i=0; i 0 i<N i N i++ i++ i {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			} {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			} if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				} max<a[i] max a[i] a i {
					max = a[i];
				} max = a[i] max a[i] a i {
					count += max-a[i];
				} 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 {
	static int N=0;
	static long count=0L;
	static long max=0L;
	public static void main(String args[]) {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	}
} public class Main {
	static int N=0;
	static long count=0L;
	static long max=0L;
	public static void main(String args[]) {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	}
} Main
import java.util.Scanner;

public class Main {
	static int N=0;
	static long count=0L;
	static long max=0L;
	public static void main(String args[]) {
		try(Scanner sc = new Scanner(System.in)){
			N = Integer.parseInt(sc.next());
			Long a[] = new Long[N]; 
			for(int i=0;i<N;i++) {
				a[i] = Long.parseLong(sc.next());
			}
			for(int i=0;i<N;i++) {
				if(max<a[i]) {
					max = a[i];
				} else {
					count += max-a[i];
				}
				
			}
		}
		System.out.println(count);
	}
}