import java.util.Scanner;

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Long[] A = new Long[n];
        Long sum = 0L;
        for(int i=0;i<n;i++){
            A[i] = sc.nextLong();
            if(i==0)continue;
            if(A[i-1]>A[i]){
                sum += A[i-1] - A[i];
                A[i] = A[i-1];
            }
        }
        System.out.println(sum);
    }
}