import java.util.*;
import java.io.*;
public class Main {

	public static void main(String[] args) throws java.lang.Exception
    {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		System.out.println(sum);
	}

} import java.util.*; util.* java import java.io.*; io.* java public class Main {

	public static void main(String[] args) throws java.lang.Exception
    {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		System.out.println(sum);
	}

} Main public static void main(String[] args) throws java.lang.Exception
    {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		System.out.println(sum);
	} main {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		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 a[]=new int[n]; a new int[n] n StringTokenizer st=new StringTokenizer(br.readLine()); st new StringTokenizer(br.readLine()) for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken()); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=Integer.parseInt(st.nextToken()); a[i]=Integer.parseInt(st.nextToken()) a[i] a i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st long sum=0; sum 0 for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		} {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		} if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			} a[i]<a[i-1] a[i] a i a[i-1] a i-1 i 1 {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			} sum+=(a[i-1]-a[i]) sum (a[i-1]-a[i]) a[i-1] a i-1 i 1 a[i] a i a[i]=a[i-1] a[i] a i a[i-1] a i-1 i 1 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 java.lang.Exception
    {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		System.out.println(sum);
	}

} public class Main {

	public static void main(String[] args) throws java.lang.Exception
    {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		System.out.println(sum);
	}

} Main
import java.util.*;
import java.io.*;
public class Main {

	public static void main(String[] args) throws java.lang.Exception
    {		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n =Integer.parseInt(br.readLine()),a[]=new int[n];
		StringTokenizer st=new StringTokenizer(br.readLine());
		for(int i=0; i<n; i++) a[i]=Integer.parseInt(st.nextToken());
		long sum=0;
		for(int i=1; i<n; i++) {
			if(a[i]<a[i-1]) {
				sum+=(a[i-1]-a[i]);
				a[i]=a[i-1];
			}
		}
		System.out.println(sum);
	}

}
