import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    }
}

class SolverC {
    public long solve(int[] arr, int N){
        long res = 0;
        for(int i=1;i<N;i++){
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        }

        return res;
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    }
} Main public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc int[] arr = new int[N]; arr new int[N] N for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            arr[i] = sc.nextInt();
        } {
            arr[i] = sc.nextInt();
        } arr[i] = sc.nextInt() arr[i] arr i sc.nextInt() sc.nextInt sc System.out.println(new SolverC().solve(arr, N)) System.out.println System.out System System.out new SolverC().solve(arr, N) new SolverC().solve new SolverC() arr N String args[] args class SolverC {
    public long solve(int[] arr, int N){
        long res = 0;
        for(int i=1;i<N;i++){
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        }

        return res;
    }
} SolverC public long solve(int[] arr, int N){
        long res = 0;
        for(int i=1;i<N;i++){
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        }

        return res;
    } solve {
        long res = 0;
        for(int i=1;i<N;i++){
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        }

        return res;
    } long res = 0; res 0 for(int i=1;i<N;i++){
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        } int i=1; int i=1; i 1 i<N i N i++ i++ i {
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        } {
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        } if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            } arr[i] < arr[i-1] arr[i] arr i arr[i-1] arr i-1 i 1 {
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            } int diff = arr[i-1] - arr[i]; diff arr[i-1] - arr[i] arr[i-1] arr i-1 i 1 arr[i] arr i arr[i] += diff arr[i] arr i diff res += diff res diff return res; res int[] arr arr int N N public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    }
} public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    }
} Main
import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] arr = new int[N];
        for(int i=0;i<N;i++){
            arr[i] = sc.nextInt();
        }

        System.out.println(new SolverC().solve(arr, N));
    }
}

class SolverC {
    public long solve(int[] arr, int N){
        long res = 0;
        for(int i=1;i<N;i++){
            if(arr[i] < arr[i-1]){
                int diff = arr[i-1] - arr[i];
                arr[i] += diff;
                res += diff;
            }
        }

        return res;
    }
}