import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	}
}		 import java.util.*; util.* java import java.io.*; io.* java public class Main
{
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	}
} Main public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	} main {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	} BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) int n=0; n 0 n=Integer.parseInt(br.readLine()) n Integer.parseInt(br.readLine()) Integer.parseInt Integer br.readLine() br.readLine br String brr[]=br.readLine().split(" "); brr br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long arr[]=new long[n]; arr new long[n] n for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		} int i=0; int i=0; i 0 i<n i n i++ i++ i {
			arr[i]=Long.parseLong(brr[i]);
		} {
			arr[i]=Long.parseLong(brr[i]);
		} arr[i]=Long.parseLong(brr[i]) arr[i] arr i Long.parseLong(brr[i]) Long.parseLong Long brr[i] brr i long ans=0; ans 0 for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		} int i=1; int i=1; i 1 i<n i n i++ i++ i {
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		} {
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		} if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			} arr[i-1]>arr[i] arr[i-1] arr i-1 i 1 arr[i] arr i {
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			} ans+=Math.abs(arr[i]-arr[i-1]) ans Math.abs(arr[i]-arr[i-1]) Math.abs Math arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 arr[i]+=Math.abs(arr[i]-arr[i-1]) arr[i] arr i Math.abs(arr[i]-arr[i-1]) Math.abs Math arr[i]-arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 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[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	}
} public class Main
{
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	}
} Main
import java.util.*;
import java.io.*;
public class Main
{
	public static void main(String args[]) throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int n=0;
		n=Integer.parseInt(br.readLine());
		String brr[]=br.readLine().split(" ");
		long arr[]=new long[n];
		for(int i=0;i<n;i++)
		{
			arr[i]=Long.parseLong(brr[i]);
		}
		long ans=0;
		for(int i=1;i<n;i++)
		{
			if(arr[i-1]>arr[i])
			{
				ans+=Math.abs(arr[i]-arr[i-1]);
				arr[i]+=Math.abs(arr[i]-arr[i-1]);
			}
		}
		System.out.println(ans);
	}
}		