import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	}

} Main public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	} main {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	} Scanner sc= new Scanner(System.in); sc new Scanner(System.in) int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int a[]=new int[n]; a new int[n] n for(int i=0;i<n;i++)
			a[i]=sc.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i]=sc.nextInt(); a[i]=sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc int max=a[0]; max a[0] a 0 long step=0; step 0 for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		} int i a {
			max=Math.max(i, max);
			step+=max-i;
		} {
			max=Math.max(i, max);
			step+=max-i;
		} max=Math.max(i, max) max Math.max(i, max) Math.max Math i max step+=max-i step max-i max i System.out.println(step) System.out.println System.out System System.out step String[] args args public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	}

} public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	}

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

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc= new Scanner(System.in);
		int n=sc.nextInt();
		int a[]=new int[n];
		for(int i=0;i<n;i++)
			a[i]=sc.nextInt();
		
		int max=a[0];
		
		long step=0;
		for(int i : a)
		{
			max=Math.max(i, max);
			step+=max-i;
		}
		System.out.println(step);
	}

}
