import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	}

} import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	}

} Main public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	} main {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int n = scan.nextInt(); n scan.nextInt() scan.nextInt scan long[] list = new long[n]; list new long[n] n long max = 0; max 0 long sum = 0; sum 0 for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			list[i] = scan.nextLong();
		} {
			list[i] = scan.nextLong();
		} list[i] = scan.nextLong() list[i] list i scan.nextLong() scan.nextLong scan for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		} {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		} if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			} max < list[i] max list[i] list i {
				max = list[i];
			} max = list[i] max list[i] list i {
				sum += max - list[i];
			} sum += max - list[i] sum max - list[i] max list[i] list i 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) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	}

} Main
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		long[] list = new long[n];
		long max = 0;
		long sum = 0;
		
		for(int i = 0; i < n; i++) {
			list[i] = scan.nextLong();
		}
		
		for(int i = 0; i < n; i++) {
			if(max < list[i]) {
				max = list[i];
			}
			else {
				sum += max - list[i];
			}
		}
		System.out.print(sum);
	}

}
