import java.util.*;
class Main {
 
	//最大公約数・最小公倍数（セットで使う）
static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
static int lcm (int a, int b) {return a*b/gcd(a,b);}
   //整数かどうかを判定する
 static boolean isInteger(double number){return Math.ceil(number) == Math.floor(number); }

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int n = sc.nextInt();
		Long[] a = new Long[n];

		for(int i=0;i<n;i++)a[i] = sc.nextLong();
		
		Long ans = 0l;
		Long max = -1l;

		for(int i=0;i<n;i++){
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		}

		
		System.out.println(ans);
	}
} import java.util.*; util.* java class Main {
 
	//最大公約数・最小公倍数（セットで使う）
static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
static int lcm (int a, int b) {return a*b/gcd(a,b);}
   //整数かどうかを判定する
 static boolean isInteger(double number){return Math.ceil(number) == Math.floor(number); }

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int n = sc.nextInt();
		Long[] a = new Long[n];

		for(int i=0;i<n;i++)a[i] = sc.nextLong();
		
		Long ans = 0l;
		Long max = -1l;

		for(int i=0;i<n;i++){
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		}

		
		System.out.println(ans);
	}
} Main //最大公約数・最小公倍数（セットで使う）
static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;} gcd {return b>0?gcd(b,a%b):a;} return b>0?gcd(b,a%b):a; b>0?gcd(b,a%b):a b>0 b 0 gcd(b,a%b) gcd b a%b a b a int a a int b b static int lcm (int a, int b) {return a*b/gcd(a,b);} lcm {return a*b/gcd(a,b);} return a*b/gcd(a,b); a*b/gcd(a,b) a*b a b gcd(a,b) gcd a b int a a int b b //整数かどうかを判定する
 static boolean isInteger(double number){return Math.ceil(number) == Math.floor(number); } isInteger {return Math.ceil(number) == Math.floor(number); } return Math.ceil(number) == Math.floor(number); Math.ceil(number) == Math.floor(number) Math.ceil(number) Math.ceil Math number Math.floor(number) Math.floor Math number double number number public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int n = sc.nextInt();
		Long[] a = new Long[n];

		for(int i=0;i<n;i++)a[i] = sc.nextLong();
		
		Long ans = 0l;
		Long max = -1l;

		for(int i=0;i<n;i++){
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		}

		
		System.out.println(ans);
	} main {
		Scanner sc = new Scanner(System.in);
 
		int n = sc.nextInt();
		Long[] a = new Long[n];

		for(int i=0;i<n;i++)a[i] = sc.nextLong();
		
		Long ans = 0l;
		Long max = -1l;

		for(int i=0;i<n;i++){
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		}

		
		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]; a new Long[n] n 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] a i sc.nextLong() sc.nextLong sc Long ans = 0l; ans 0l Long max = -1l; max -1l 1l for(int i=0;i<n;i++){
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		} {
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		} if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			} max < a[i] max a[i] a i {
				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 System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.*;
class Main {
 
	//最大公約数・最小公倍数（セットで使う）
static int gcd (int a, int b) {return b>0?gcd(b,a%b):a;}
static int lcm (int a, int b) {return a*b/gcd(a,b);}
   //整数かどうかを判定する
 static boolean isInteger(double number){return Math.ceil(number) == Math.floor(number); }

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
 
		int n = sc.nextInt();
		Long[] a = new Long[n];

		for(int i=0;i<n;i++)a[i] = sc.nextLong();
		
		Long ans = 0l;
		Long max = -1l;

		for(int i=0;i<n;i++){
			if(max < a[i]){
				max = a[i];
			} else {
				ans += max - a[i];
			}
		}

		
		System.out.println(ans);
	}
}