
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	}
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

	public static void main(String[] args) {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	}
} Main public static void main(String[] args) {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	} main {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	} Scanner ob=new Scanner(System.in); ob new Scanner(System.in) int n=ob.nextInt(); n ob.nextInt() ob.nextInt ob long a[]=new long[n]; a new long[n] n for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		} int i=0; int i=0; i 0 i<a.length i a.length a a.length i++ i++ i {
			a[i]=ob.nextLong();
		} {
			a[i]=ob.nextLong();
		} a[i]=ob.nextLong() a[i] a i ob.nextLong() ob.nextLong ob long sum=0; sum 0 for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		} int i=0; int i=0; i 0 i<a.length-1 i a.length-1 a.length a a.length 1 i++ i++ i {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		} {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		} if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			} a[i]>a[i+1] a[i] a i a[i+1] a i+1 i 1 {
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			} long d=a[i]-a[i+1]; d a[i]-a[i+1] a[i] a i a[i+1] a i+1 i 1 sum=sum+d sum sum+d sum d a[i+1]=a[i] a[i+1] a i+1 i 1 a[i] a i 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) {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	}
} public class Main {

	public static void main(String[] args) {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	}
} Main

import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner ob=new Scanner(System.in);
		int n=ob.nextInt();
		long a[]=new long[n];
		for (int i=0; i<a.length; i++) {
			a[i]=ob.nextLong();
		}
		long sum=0;
		for (int i=0; i<a.length-1; i++) {
			if(a[i]>a[i+1])
			{
			long d=a[i]-a[i+1];
			sum=sum+d;
			a[i+1]=a[i];
			}
		}
		System.out.println(sum);
	}
}
