

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args)throws Exception {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        System.out.println(sum);
        
	        
	}

	

} import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io import java.io.PrintWriter; PrintWriter java.io import java.math.BigInteger; BigInteger java.math import java.util.Scanner; Scanner java.util import java.util.StringTokenizer; StringTokenizer java.util public class Main {

	public static void main(String[] args)throws Exception {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        System.out.println(sum);
        
	        
	}

	

} Main public static void main(String[] args)throws Exception {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        System.out.println(sum);
        
	        
	} main {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        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 StringTokenizer st = new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) int max=-1; max -1 1 long sum=0; sum 0 for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        } {
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        } int k = Integer.parseInt(st.nextToken()); k Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	} i==0 i 0 {
        		max=k;
        	} max=k max k {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	} if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		} max>k max k {
        			sum+=max-k;
        			k=max;
        		} sum+=max-k sum max-k max k k=max k max {
        			max=Math.max(k, max);
        		} max=Math.max(k, max) max Math.max(k, max) Math.max Math k max 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 {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        System.out.println(sum);
        
	        
	}

	

} public class Main {

	public static void main(String[] args)throws Exception {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        System.out.println(sum);
        
	        
	}

	

} Main


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args)throws Exception {
//
//		Scanner sc = new Scanner(System.in);
//		BigInteger n = sc.nextBigInteger();
//	      boolean flag=solve(n);
//	      if(flag==true)
//	        System.out.println("Yes");
//	      else
//	    	  System.out.println("No");
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		int n = Integer.parseInt(br.readLine());
        StringTokenizer st = new StringTokenizer(br.readLine());
        int max=-1;
        long sum=0;
        for(int i=0; i<n; i++){
        	int k = Integer.parseInt(st.nextToken());
        	if(i==0) {
        		max=k;
        	}
        	else {
        		if(max>k) {
        			sum+=max-k;
        			k=max;
        		}
        		else {
        			max=Math.max(k, max);
        		}
        	}
        }
        System.out.println(sum);
        
	        
	}

	

}
