import java.util.Scanner;

public class Main {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	}	

} import java.util.Scanner; Scanner java.util public class Main {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	}	

} Main private static Scanner sc; sc public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	} main {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	} sc = new Scanner(System.in) sc new Scanner(System.in) int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc long result = 0; result 0 long nowHighestLength = 0; nowHighestLength 0 for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		} int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		} {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		} long numberILength = Long.parseLong(sc.next()); numberILength Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			} nowHighestLength < numberILength nowHighestLength numberILength {
				nowHighestLength = numberILength;
			} nowHighestLength = numberILength nowHighestLength numberILength {
				result += nowHighestLength - numberILength;
			} result += nowHighestLength - numberILength result nowHighestLength - numberILength nowHighestLength numberILength System.out.println(result) System.out.println System.out System System.out result String[] args args public class Main {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	}	

} public class Main {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	}	

} Main
import java.util.Scanner;

public class Main {
	private static Scanner sc;

	public static void main(String[] args) {
		sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long result = 0;
		long nowHighestLength = 0;
		for (int i = 0; i < n; i++) {
			long numberILength = Long.parseLong(sc.next());
			if (nowHighestLength < numberILength) {
				nowHighestLength = numberILength;
			} else {
				result += nowHighestLength - numberILength;
			}
		}
		System.out.println(result);
		
		
	}	

}
