import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	}
	
} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	}
	
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	} main {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N = Integer.parseInt(scan.next()); N Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int preA = Integer.parseInt(scan.next()); preA Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan int postA = 0; postA 0 long sum=0; sum 0 for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		} int i=1; int i=1; i 1 i<N i N i++ i++ i {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		} {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		} postA = Integer.parseInt(scan.next()) postA Integer.parseInt(scan.next()) Integer.parseInt Integer scan.next() scan.next scan if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			} preA > postA preA postA {
				sum += preA - postA;
			} sum += preA - postA sum preA - postA preA postA {
				preA = postA;
			} preA = postA preA postA System.out.print(sum) System.out.print System.out System System.out sum String[] args args public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	}
	
} public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	}
	
} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = Integer.parseInt(scan.next());
		int preA = Integer.parseInt(scan.next());
		int postA = 0;
		long sum=0;
		for(int i=1;i<N;i++) {
			postA = Integer.parseInt(scan.next());
			if(preA > postA) {
				sum += preA - postA;
			} else {
				preA = postA;
			}
			
		}
		System.out.print(sum);
	}
	
}