import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    }
} import java.util.Arrays; Arrays java.util import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    }
} Main public static void main(String[] args) {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    } main {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int        N = scan.nextInt(); N scan.nextInt() scan.nextInt scan long A[]=new long [N]; A new long [N] N long p=0; p 0 for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

} int i=0; int i=0; i 0 i<N i N i++ i++ i {
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

} {
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

} A[i]=scan.nextLong() A[i] A i scan.nextLong() scan.nextLong scan if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}} i>0 i 0 {if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}} if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];} A[i]<A[i-1] A[i] A i A[i-1] A i-1 i 1 {p=p+A[i-1]-A[i];
    A[i]=A[i-1];} p=p+A[i-1]-A[i] p p+A[i-1]-A[i] p+A[i-1] p A[i-1] A i-1 i 1 A[i] A i A[i]=A[i-1] A[i] A i A[i-1] A i-1 i 1 System.out.println(p) System.out.println System.out System System.out p String[] args args public class Main {

    public static void main(String[] args) {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    }
} public class Main {

    public static void main(String[] args) {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    }
} Main
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
	// write your code here

        Scanner scan = new Scanner(System.in);
int        N = scan.nextInt();
long A[]=new long [N];
long p=0;
for(int i=0;i<N;i++){
    A[i]=scan.nextLong();
    if(i>0){if(A[i]<A[i-1]){p=p+A[i-1]-A[i];
    A[i]=A[i-1];}}

}
System.out.println(p);
    }
}
