import java.math.BigInteger;
import java.util.*;
public class Main{
	public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	}
}
 import java.math.BigInteger; BigInteger java.math import java.util.*; util.* java public class Main{
	public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	}
} Main public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	} main {
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	} Scanner sc= new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc BigInteger a[] = new BigInteger[n]; a new BigInteger[n] n BigInteger stool = BigInteger.ZERO; stool BigInteger.ZERO BigInteger BigInteger.ZERO for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i] = sc.nextBigInteger(); a[i] = sc.nextBigInteger() a[i] a i sc.nextBigInteger() sc.nextBigInteger sc for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        } {
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        } if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	} a[i].compareTo(a[i-1])==-1 a[i].compareTo(a[i-1]) a[i].compareTo a[i] a i a[i-1] a i-1 i 1 -1 1 {
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	} BigInteger x = (a[i-1].subtract(a[i])); x (a[i-1].subtract(a[i])) a[i-1].subtract a[i-1] a i-1 i 1 a[i] a i stool = stool.add(x) stool stool.add(x) stool.add stool x a[i] = a[i-1] a[i] a i a[i-1] a i-1 i 1 System.out.println(stool) System.out.println System.out System System.out stool String[] args args public class Main{
	public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	}
} public class Main{
	public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	}
} Main
import java.math.BigInteger;
import java.util.*;
public class Main{
	public static void main(String[] args){
        Scanner sc= new Scanner(System.in);
        int n = sc.nextInt();
        BigInteger a[] = new BigInteger[n];
        BigInteger stool = BigInteger.ZERO;
        for(int i=0;i<n;i++)
            a[i] = sc.nextBigInteger();

        for(int i=1;i<n;i++){
        	if(a[i].compareTo(a[i-1])==-1){
        		BigInteger x = (a[i-1].subtract(a[i]));
        		stool = stool.add(x);
        		a[i] = a[i-1];
        	}
        }
        System.out.println(stool);

  	}
}

