import java.io.*;
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
         int n=in.nextInt();
         long a[]=new long[n];
         for(int i=0;i<n;i++){
            a[i]=in.nextLong();
         }
         long sum=0;
         for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum=sum+a[i-1]-a[i];
                a[i]=a[i-1];
            }
         }
         System.out.println(sum);
    }
}
class scan
{
    public static BufferedReader reader;
    public static StringTokenizer token;

    scan(InputStream str)
    {
        reader=new BufferedReader(new InputStreamReader(str));
        token=null;
    }

    static int nextInt()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Integer.parseInt(token.nextToken());
    }

    static long nextLong()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Long.parseLong(token.nextToken());
    }

    static String next()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return token.nextToken();
    }

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

    scan(InputStream str)
    {
        reader=new BufferedReader(new InputStreamReader(str));
        token=null;
    }

    static int nextInt()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Integer.parseInt(token.nextToken());
    }

    static long nextLong()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Long.parseLong(token.nextToken());
    }

    static String next()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return token.nextToken();
    }

} scan public static BufferedReader reader; reader public static StringTokenizer token; token scan(InputStream str)
    {
        reader=new BufferedReader(new InputStreamReader(str));
        token=null;
    } scan {
        reader=new BufferedReader(new InputStreamReader(str));
        token=null;
    } reader=new BufferedReader(new InputStreamReader(str)) reader new BufferedReader(new InputStreamReader(str)) token=null token null InputStream str str static int nextInt()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Integer.parseInt(token.nextToken());
    } nextInt {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Integer.parseInt(token.nextToken());
    } while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        } token==null||!token.hasMoreTokens() token==null token null !token.hasMoreTokens() token.hasMoreTokens() token.hasMoreTokens token {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        } try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); } catch(IOException e){  throw new RuntimeException(e); } IOException e {  throw new RuntimeException(e); } throw new RuntimeException(e); new RuntimeException(e) { token=new StringTokenizer(reader.readLine());   } token=new StringTokenizer(reader.readLine()) token new StringTokenizer(reader.readLine()) return Integer.parseInt(token.nextToken()); Integer.parseInt(token.nextToken()) Integer.parseInt Integer token.nextToken() token.nextToken token static long nextLong()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Long.parseLong(token.nextToken());
    } nextLong {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Long.parseLong(token.nextToken());
    } while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        } token==null||!token.hasMoreTokens() token==null token null !token.hasMoreTokens() token.hasMoreTokens() token.hasMoreTokens token {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        } try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); } catch(IOException e){  throw new RuntimeException(e); } IOException e {  throw new RuntimeException(e); } throw new RuntimeException(e); new RuntimeException(e) { token=new StringTokenizer(reader.readLine());   } token=new StringTokenizer(reader.readLine()) token new StringTokenizer(reader.readLine()) return Long.parseLong(token.nextToken()); Long.parseLong(token.nextToken()) Long.parseLong Long token.nextToken() token.nextToken token static String next()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return token.nextToken();
    } next {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return token.nextToken();
    } while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        } token==null||!token.hasMoreTokens() token==null token null !token.hasMoreTokens() token.hasMoreTokens() token.hasMoreTokens token {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        } try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); } catch(IOException e){  throw new RuntimeException(e); } IOException e {  throw new RuntimeException(e); } throw new RuntimeException(e); new RuntimeException(e) { token=new StringTokenizer(reader.readLine());   } token=new StringTokenizer(reader.readLine()) token new StringTokenizer(reader.readLine()) return token.nextToken(); token.nextToken() token.nextToken token public class Main {
    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
         int n=in.nextInt();
         long a[]=new long[n];
         for(int i=0;i<n;i++){
            a[i]=in.nextLong();
         }
         long sum=0;
         for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum=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 in=new Scanner(System.in);
         int n=in.nextInt();
         long a[]=new long[n];
         for(int i=0;i<n;i++){
            a[i]=in.nextLong();
         }
         long sum=0;
         for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum=sum+a[i-1]-a[i];
                a[i]=a[i-1];
            }
         }
         System.out.println(sum);
    }
} Main
import java.io.*;
import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner in=new Scanner(System.in);
         int n=in.nextInt();
         long a[]=new long[n];
         for(int i=0;i<n;i++){
            a[i]=in.nextLong();
         }
         long sum=0;
         for(int i=1;i<n;i++){
            if(a[i]<a[i-1]){
                sum=sum+a[i-1]-a[i];
                a[i]=a[i-1];
            }
         }
         System.out.println(sum);
    }
}
class scan
{
    public static BufferedReader reader;
    public static StringTokenizer token;

    scan(InputStream str)
    {
        reader=new BufferedReader(new InputStreamReader(str));
        token=null;
    }

    static int nextInt()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Integer.parseInt(token.nextToken());
    }

    static long nextLong()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return Long.parseLong(token.nextToken());
    }

    static String next()
    {
        while(token==null||!token.hasMoreTokens())
        {
            try { token=new StringTokenizer(reader.readLine());   }
            catch(IOException e){  throw new RuntimeException(e); }
        }
        return token.nextToken();
    }

}