import java.util.*;
import java.math.*;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    }
} import java.util.*; util.* java import java.math.*; math.* java public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    }
} Main public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    } main {
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc BigInteger[] arr = new BigInteger[N]; arr new BigInteger[N] N BigInteger res = new BigInteger("0"); res new BigInteger("0") for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        } {
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        } arr[i] = sc.nextBigInteger() arr[i] arr i sc.nextBigInteger() sc.nextBigInteger sc if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            } (i > 0) && (arr[i].compareTo(arr[i-1]) == -1) (i > 0) i 0 (arr[i].compareTo(arr[i-1]) == -1) arr[i].compareTo(arr[i-1]) arr[i].compareTo arr[i] arr i arr[i-1] arr i-1 i 1 -1 1 {
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            } BigInteger x = arr[i-1].subtract(arr[i]); x arr[i-1].subtract(arr[i]) arr[i-1].subtract arr[i-1] arr i-1 i 1 arr[i] arr i arr[i] = arr[i].add(x) arr[i] arr i arr[i].add(x) arr[i].add arr[i] arr i x res = res.add(x) res res.add(x) res.add res x 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){
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    }
} public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    }
} Main
import java.util.*;
import java.math.*;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      	int N = sc.nextInt();
      	BigInteger[] arr = new BigInteger[N];
      	BigInteger res = new BigInteger("0");
      	for (int i = 0; i < N; i++){
        	arr[i] = sc.nextBigInteger();
          	if ((i > 0) && (arr[i].compareTo(arr[i-1]) == -1)){
            	BigInteger x = arr[i-1].subtract(arr[i]);
              	arr[i] = arr[i].add(x);
            	res = res.add(x);
            }
        }
      	
      	System.out.println(res);
    }
}