import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	}
}

 import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	} main {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long sumStep = 0; sumStep 0 int maxA = sc.nextInt(); maxA sc.nextInt() sc.nextInt sc for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		} int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		} {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		} int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc maxA = Math.max(maxA, a) maxA Math.max(maxA, a) Math.max Math maxA a sumStep += maxA - a sumStep maxA - a maxA a System.out.println(sumStep) System.out.println System.out System System.out sumStep sc.close() sc.close sc String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		
		long sumStep = 0;
		int maxA = sc.nextInt();
		for (int i = 1; i < n; i++) {
			int a = sc.nextInt();
			maxA = Math.max(maxA, a);
			sumStep += maxA - a;
		}
		
		System.out.println(sumStep);
		
		sc.close();
	}
}


