/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	}
} Main public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	} main {
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	} Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner long []  arr = new long[n]; arr new long[n] n for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
	    	arr[i] = scanner.nextLong();
	    } {
	    	arr[i] = scanner.nextLong();
	    } arr[i] = scanner.nextLong() arr[i] arr i scanner.nextLong() scanner.nextLong scanner long res = 0; res 0 for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    } {
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    } if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	} arr[i] < arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 {
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	} res += arr[i-1] - arr[i] res arr[i-1] - arr[i] arr[i-1] arr i-1 i 1 arr[i] arr i arr[i] += arr[i-1] - arr[i] arr[i] arr i arr[i-1] - arr[i] arr[i-1] arr i-1 i 1 arr[i] arr i System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	}
} public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	}
} Main
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner scanner = new Scanner(System.in);
	    int n = scanner.nextInt();
	    
	    long []  arr = new long[n];
	    for(int i = 0; i < n; i++){
	    	arr[i] = scanner.nextLong();
	    }
	    
	    long res = 0;
	    for(int i = 1; i < n ; i++){
	    	if(arr[i] < arr[i-1]){
	    		res += arr[i-1] - arr[i];
	    		arr[i] += arr[i-1] - arr[i]; 
	    	}
	    }
	    
	    System.out.println(res);
	}
}