import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String args[]) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    }
} Main public static void main(String args[]) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    } main {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    } Scanner s=new Scanner(System.in); s new Scanner(System.in) int n=s.nextInt(); n s.nextInt() s.nextInt s long ans=0; ans 0 int prev=s.nextInt(); prev s.nextInt() s.nextInt s for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        } {
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        } int ele=s.nextInt(); ele s.nextInt() s.nextInt s if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            } ele<prev ele prev {
                ans+=prev-ele;
            } ans+=prev-ele ans prev-ele prev ele {
                prev=ele;
            } prev=ele prev ele System.out.print(ans) System.out.print System.out System System.out ans String args[] args public class Main {
    public static void main(String args[]) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    }
} public class Main {
    public static void main(String args[]) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    }
} Main
import java.util.Scanner;
public class Main {
    public static void main(String args[]) {
        Scanner s=new Scanner(System.in);
        int n=s.nextInt();
        long ans=0;
        int prev=s.nextInt();
        for(int i=1;i<n;i++){
            int ele=s.nextInt();
            if(ele<prev){
                ans+=prev-ele;
            }
            else{
                prev=ele;
            }
        }

        System.out.print(ans);
    }
}