
import java.util.*;

public class Main {

	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	}
	
} import java.util.*; util.* java public class Main {

	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	}
	
} Main public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	} main {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int n=sc.nextInt(); n sc.nextInt() sc.nextInt sc int ar[]=new int[n]; ar new int[n] n for(int i=0;i<n;i++) ar[i]=sc.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i ar[i]=sc.nextInt(); ar[i]=sc.nextInt() ar[i] ar i sc.nextInt() sc.nextInt sc long preh=ar[0] preh ar[0] ar 0 table table ans=0; ans 0 for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		} {
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		} table=preh-ar[i] table preh-ar[i] preh ar[i] ar i if(table<0) table=0; table<0 table 0 table=0 table 0 ans+=table ans table preh=table+ar[i] preh table+ar[i] table ar[i] ar i System.out.println(ans) System.out.println System.out System System.out ans String args[] args public class Main {

	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	}
	
} public class Main {

	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	}
	
} Main

import java.util.*;

public class Main {

	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int ar[]=new int[n];
		for(int i=0;i<n;i++) ar[i]=sc.nextInt();
		
		long preh=ar[0],table,ans=0;
		for(int i=0;i<n;i++)
		{
			table=preh-ar[i];
			if(table<0) table=0;
			ans+=table;
			preh=table+ar[i];
		}
		System.out.println(ans);
	}
	
}
