import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

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[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	}
} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.math.BigInteger; BigInteger java.math 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[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	}
} Main public static void main(String[] args)throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		String[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	} 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[] strs  = br.readLine().split(" "); strs br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int max = 0; max 0 BigInteger sum = new BigInteger("0"); sum new BigInteger("0") for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		} String str strs {
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		} {
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		} int n = Integer.parseInt(str); n Integer.parseInt(str) Integer.parseInt Integer str if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			} n < max n max {
				sum =  sum.add(new BigInteger("" + (max - n)));
			} sum =  sum.add(new BigInteger("" + (max - n))) sum sum.add(new BigInteger("" + (max - n))) sum.add sum new BigInteger("" + (max - n)) {
				max = n;
			} max = n max n System.out.println(sum) System.out.println System.out System System.out sum 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[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	}
} 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[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	}
} Main
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

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[] strs  = br.readLine().split(" ");
		
		int max = 0;
		BigInteger sum = new BigInteger("0");
		for(String str : strs){
			int n = Integer.parseInt(str);
			if(n < max){
				sum =  sum.add(new BigInteger("" + (max - n)));
			}else{
				max = n;
			}
		}
		
		System.out.println(sum);
	}
}
