import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	}
} Main public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	} main {
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long size = sc.nextInt(); size sc.nextInt() sc.nextInt sc long max = 0; max 0 long total = 0; total 0 for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		} long i=0; long i=0; i 0 i<size i size i++ i++ i {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		} {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		} long sci = sc.nextInt(); sci sc.nextInt() sc.nextInt sc if(max < sci) max = sci;
			else if(max > sci) total += max - sci; max < sci max sci max = sci max sci if(max > sci) total += max - sci; max > sci max sci total += max - sci total max - sci max sci System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	}
} public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		long size = sc.nextInt();
		long max = 0;
		long total = 0;

		for(long i=0; i<size; i++) {
			long sci = sc.nextInt();
			if(max < sci) max = sci;
			else if(max > sci) total += max - sci;
		}
		
		System.out.println(total);
	}
}
