import java.util.*;

public class Main{
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args){
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    }
} import java.util.*; util.* java public class Main{
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args){
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    }
} Main static Scanner in = new Scanner(System.in); in new Scanner(System.in) public static void main(String[] args){
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    } main {
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    } int n = in.nextInt(); n in.nextInt() in.nextInt in int[] arr = new int[n]; arr new int[n] n for(int i = 0; i < n; i++)
           arr[i] = in.nextInt(); int i = 0; int i = 0; i 0 i < n i n i++ i++ i arr[i] = in.nextInt(); arr[i] = in.nextInt() arr[i] arr i in.nextInt() in.nextInt in long count = 0; count 0 int max = arr[0]; max arr[0] arr 0 for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        } {
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        } if(arr[i] < max)
                count += (max - arr[i]); arr[i] < max arr[i] arr i max count += (max - arr[i]) count (max - arr[i]) max arr[i] arr i max = Math.max(max, arr[i]) max Math.max(max, arr[i]) Math.max Math max arr[i] arr i System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main{
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args){
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    }
} public class Main{
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args){
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    }
} Main
import java.util.*;

public class Main{
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args){
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
           arr[i] = in.nextInt();

        long count = 0;
        int max = arr[0];
        for(int i = 1; i < n; i++){
            if(arr[i] < max)
                count += (max - arr[i]);
            max = Math.max(max, arr[i]);
        }
        System.out.println(count);
    }
}
