import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Long[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

	}
}
 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[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

	}
} Main public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Long[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

	} main {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Long[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

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

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Long[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

	}
} public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Long[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

	}
} 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[] a = new Long[n + 1];
		for(int i = 0 ; i < n ; i++) {
			a[i] = sc.nextLong();
		}
		long max = 0;
		long ans = 0;
		for(int i = 0 ; i < n ; i++) {
			if(max > a[i]) {
				ans += max - a[i];
			}
			if(max < a[i]) {
				max = a[i];
			}
		}
		sc.close();
		System.out.println(ans);

	}
}

