import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    }
} 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[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    } main {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] a = new long[n]; a new long[n] n 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 long len = 0; len 0 for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        } {
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        } if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            } a[i-1] > a[i] a[i-1] a i-1 i 1 a[i] a i {
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            } len += a[i-1] - a[i] len a[i-1] - a[i] a[i-1] a i-1 i 1 a[i] a i a[i] += a[i-1]-a[i] a[i] a i a[i-1]-a[i] a[i-1] a i-1 i 1 a[i] a i System.out.println(len) System.out.println System.out System System.out len String[] args args public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    }
} public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt();
        }
        long len = 0;
        for(int i = 1; i < n; i++){
            if(a[i-1] > a[i]){
                len += a[i-1] - a[i];
                a[i] += a[i-1]-a[i];
            }
        }
        System.out.println(len);
    }
}