import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);    
    public static void main(String[] args) {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    static Scanner sc = new Scanner(System.in);    
    public static void main(String[] args) {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    }
} Main static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[] args) {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    } main {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    } int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] arr = new long[n]; arr new long[n] n for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
            arr[i] = sc.nextLong();
        } {
            arr[i] = sc.nextLong();
        } arr[i] = sc.nextLong() arr[i] arr i sc.nextLong() sc.nextLong sc long highest = 0 highest 0 ans = 0; ans 0 for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } long a arr {
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } {
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            } a < highest a highest {
                ans += highest - a;
            } ans += highest - a ans highest - a highest a {
                highest = a;
            } highest = a highest a System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
    static Scanner sc = new Scanner(System.in);    
    public static void main(String[] args) {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    }
} public class Main {
    static Scanner sc = new Scanner(System.in);    
    public static void main(String[] args) {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    }
} Main
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);    
    public static void main(String[] args) {
        int n = sc.nextInt();
        long[] arr = new long[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextLong();
        }
        long highest = 0, ans = 0;
        for (long a: arr){
            if (a < highest){
                ans += highest - a;
            }else{
                highest = a;
            }
        } 
        System.out.println(ans);
    }
}