import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    }
} Main public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    } main {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int A[] = new int[N]; A new int[N] N long total = 0; total 0 for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            A[i] = sc.nextInt();
        } {
            A[i] = sc.nextInt();
        } A[i] = sc.nextInt() A[i] A i sc.nextInt() sc.nextInt sc for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        } int i = 0; int i = 0; i 0 i < N-1 i N-1 N 1 i++ i++ i {
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        } {
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        } for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            } int j = i+1; int j = i+1; j i+1 i 1 j < i+2 j i+2 i 2 j++ j++ j {
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            } {
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            } if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                } A[i] == A[j] || A[i] < A[j] A[i] == A[j] A[i] A i A[j] A j A[i] < A[j] A[i] A i A[j] A j {
                    continue;
                } continue; if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                } A[i] > A[j] A[i] A i A[j] A j {
                    total += A[i] - A[j];
                    A[j] = A[i];
                } total += A[i] - A[j] total A[i] - A[j] A[i] A i A[j] A j A[j] = A[i] A[j] A j A[i] A i System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    }
} public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A[] = new int[N];
        long total = 0;
        for(int i = 0; i < N; i++){
            A[i] = sc.nextInt();
        }
        for(int i = 0; i < N-1; i++){
            for(int j = i+1; j < i+2; j++){
                if(A[i] == A[j] || A[i] < A[j]){
                    continue;
                }
                if(A[i] > A[j]){
                    total += A[i] - A[j];
                    A[j] = A[i];
                }
            }
        }
        System.out.println(total);
    }
}
