//package abc176;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	}
	
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	}
	
} Main public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int N = Integer.parseInt(br.readLine()); N Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String[] ip = br.readLine().split(" "); ip br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int[] ary = new int[N]; ary new int[N] N for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		} int i = 0 ; int i = 0 ; i 0 i < N i N i++ i++ i {
			ary[i] = Integer.parseInt(ip[i]);
		} {
			ary[i] = Integer.parseInt(ip[i]);
		} ary[i] = Integer.parseInt(ip[i]) ary[i] ary i Integer.parseInt(ip[i]) Integer.parseInt Integer ip[i] ip i int prev = ary[0]; prev ary[0] ary 0 long ans = 0; ans 0 for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		} int i = 1 ; int i = 1 ; i 1 i < N i N i++ i++ i {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		} {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		} if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			} prev > ary[i] prev ary[i] ary i {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			} ans += Math.abs(prev-ary[i]) ans Math.abs(prev-ary[i]) Math.abs Math prev-ary[i] prev ary[i] ary i ary[i] += Math.abs(prev-ary[i]) ary[i] ary i Math.abs(prev-ary[i]) Math.abs Math prev-ary[i] prev ary[i] ary i prev = Math.max(ary[i], prev) prev Math.max(ary[i], prev) Math.max Math ary[i] ary i prev 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) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	}
	
} public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	}
	
} Main
//package abc176;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] ip = br.readLine().split(" ");
		int[] ary = new int[N];
		
		for(int i = 0 ; i < N ; i++) {
			ary[i] = Integer.parseInt(ip[i]);
		}
		
		int prev = ary[0];
		long ans = 0;
		
		for(int i = 1 ; i < N ; i++) {
			if(prev > ary[i]) {
				ans += Math.abs(prev-ary[i]);
				ary[i] += Math.abs(prev-ary[i]);
			}
			prev = Math.max(ary[i], prev);
		}
		
		System.out.println(ans);
	}
	
}
