import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    }
} import java.util.*; util.* java public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    } main {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int N = sc.nextInt(); N sc.nextInt() sc.nextInt sc long result[] = new long[N]; result new long[N] N long Comparison[] = new long[N]; Comparison new long[N] N long min; min long anc = 0; anc 0 for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        } {
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        } result[i] = sc.nextInt() result[i] result i sc.nextInt() sc.nextInt sc if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            } i < 1 i 1 {
                Comparison[i] = result[i];
            } Comparison[i] = result[i] Comparison[i] Comparison i result[i] result i {
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            } if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                } Comparison[i - 1] < result[i] Comparison[i - 1] Comparison i - 1 i 1 result[i] result i {
                    Comparison[i] = result[i];
                } Comparison[i] = result[i] Comparison[i] Comparison i result[i] result i {
                    Comparison[i] = Comparison[i-1];
                } Comparison[i] = Comparison[i-1] Comparison[i] Comparison i Comparison[i-1] Comparison i-1 i 1 for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        } {
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        } if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            } result[i] != Comparison[i] result[i] result i Comparison[i] Comparison i {
                min = Comparison[i] - result[i];
                anc += min;
            } min = Comparison[i] - result[i] min Comparison[i] - result[i] Comparison[i] Comparison i result[i] result i anc += min anc min System.out.println(anc) System.out.println System.out System System.out anc String[] args args public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    }
} public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    }
} Main
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long result[] = new long[N];
        long Comparison[] = new long[N];
        long min;
        long anc = 0;
        for(int i = 0;i < N;i++){
            result[i] = sc.nextInt();
            if(i < 1){
                Comparison[i] = result[i];
            }else{
                if(Comparison[i - 1] < result[i]){
                    Comparison[i] = result[i];
                }else{
                    Comparison[i] = Comparison[i-1];
                }
            }
        }
        for(int i = 0;i < N;i++){
            if(result[i] != Comparison[i]){
                min = Comparison[i] - result[i];
                anc += min;
            }
        }
        System.out.println(anc);
    }
}
