import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.math.BigInteger; 
 public class Main
{
     static Scanner sc=new Scanner(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    static void pn(Object o)
     {
    out.println(o);
    out.flush();
    
    }
    static void p(Object o)
    {
    out.print(o);
    out.flush();
    }
    static void pni(Object o)
    {
    out.println(o);System.out.flush();
    }
        static int I() throws IOException
    { 
    return sc.nextInt();
    }
    static long L()throws IOException
    {
    return sc.nextLong();
    }
    static double D()throws IOException
    {
    return sc.nextDouble();
    }
    static String S()throws IOException
    {
    return sc.next();
        }
        static void process() throws IOException
     {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        }
         public static void main(String[] args) throws IOException
  {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
}
             
         
       }
    import java.io.IOException; IOException java.io import java.io.PrintWriter; PrintWriter java.io import java.util.Scanner; Scanner java.util import java.math.BigInteger; BigInteger java.math public class Main
{
     static Scanner sc=new Scanner(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    static void pn(Object o)
     {
    out.println(o);
    out.flush();
    
    }
    static void p(Object o)
    {
    out.print(o);
    out.flush();
    }
    static void pni(Object o)
    {
    out.println(o);System.out.flush();
    }
        static int I() throws IOException
    { 
    return sc.nextInt();
    }
    static long L()throws IOException
    {
    return sc.nextLong();
    }
    static double D()throws IOException
    {
    return sc.nextDouble();
    }
    static String S()throws IOException
    {
    return sc.next();
        }
        static void process() throws IOException
     {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        }
         public static void main(String[] args) throws IOException
  {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
}
             
         
       } Main static Scanner sc=new Scanner(System.in); sc new Scanner(System.in) static PrintWriter out=new PrintWriter(System.out); out new PrintWriter(System.out) static void pn(Object o)
     {
    out.println(o);
    out.flush();
    
    } pn {
    out.println(o);
    out.flush();
    
    } out.println(o) out.println out o out.flush() out.flush out Object o o static void p(Object o)
    {
    out.print(o);
    out.flush();
    } p {
    out.print(o);
    out.flush();
    } out.print(o) out.print out o out.flush() out.flush out Object o o static void pni(Object o)
    {
    out.println(o);System.out.flush();
    } pni {
    out.println(o);System.out.flush();
    } out.println(o) out.println out o System.out.flush() System.out.flush System.out System System.out Object o o static int I() throws IOException
    { 
    return sc.nextInt();
    } I { 
    return sc.nextInt();
    } return sc.nextInt(); sc.nextInt() sc.nextInt sc static long L()throws IOException
    {
    return sc.nextLong();
    } L {
    return sc.nextLong();
    } return sc.nextLong(); sc.nextLong() sc.nextLong sc static double D()throws IOException
    {
    return sc.nextDouble();
    } D {
    return sc.nextDouble();
    } return sc.nextDouble(); sc.nextDouble() sc.nextDouble sc static String S()throws IOException
    {
    return sc.next();
        } S {
    return sc.next();
        } return sc.next(); sc.next() sc.next sc static void process() throws IOException
     {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        } process {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        } int n=I(); n I() I long a[]=new long[n]; a new long[n] n long c=0; c 0 for(int i=0;i<n;i++)
         {
             a[i]=L();
            } int i=0; int i=0; i 0 i<n i n i++ i++ i {
             a[i]=L();
            } {
             a[i]=L();
            } a[i]=L() a[i] a i L() L for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+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])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            } {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            } if(a[i]<a[i-1])
            {
                c=c+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 {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            } c=c+a[i-1]-a[i] c c+a[i-1]-a[i] c+a[i-1] c 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 pn(c) pn c public static void main(String[] args) throws IOException
  {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
} main {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
} try
{
        process();
 }
catch(Exception e)
{
return;
} catch(Exception e)
{
return;
} Exception e {
return;
} return; {
        process();
 } process() process String[] args args public class Main
{
     static Scanner sc=new Scanner(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    static void pn(Object o)
     {
    out.println(o);
    out.flush();
    
    }
    static void p(Object o)
    {
    out.print(o);
    out.flush();
    }
    static void pni(Object o)
    {
    out.println(o);System.out.flush();
    }
        static int I() throws IOException
    { 
    return sc.nextInt();
    }
    static long L()throws IOException
    {
    return sc.nextLong();
    }
    static double D()throws IOException
    {
    return sc.nextDouble();
    }
    static String S()throws IOException
    {
    return sc.next();
        }
        static void process() throws IOException
     {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        }
         public static void main(String[] args) throws IOException
  {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
}
             
         
       } public class Main
{
     static Scanner sc=new Scanner(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    static void pn(Object o)
     {
    out.println(o);
    out.flush();
    
    }
    static void p(Object o)
    {
    out.print(o);
    out.flush();
    }
    static void pni(Object o)
    {
    out.println(o);System.out.flush();
    }
        static int I() throws IOException
    { 
    return sc.nextInt();
    }
    static long L()throws IOException
    {
    return sc.nextLong();
    }
    static double D()throws IOException
    {
    return sc.nextDouble();
    }
    static String S()throws IOException
    {
    return sc.next();
        }
        static void process() throws IOException
     {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        }
         public static void main(String[] args) throws IOException
  {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
}
             
         
       } Main
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.math.BigInteger; 
 public class Main
{
     static Scanner sc=new Scanner(System.in);
    static PrintWriter out=new PrintWriter(System.out);
    static void pn(Object o)
     {
    out.println(o);
    out.flush();
    
    }
    static void p(Object o)
    {
    out.print(o);
    out.flush();
    }
    static void pni(Object o)
    {
    out.println(o);System.out.flush();
    }
        static int I() throws IOException
    { 
    return sc.nextInt();
    }
    static long L()throws IOException
    {
    return sc.nextLong();
    }
    static double D()throws IOException
    {
    return sc.nextDouble();
    }
    static String S()throws IOException
    {
    return sc.next();
        }
        static void process() throws IOException
     {
         int n=I();
         long a[]=new long[n];long c=0;
         for(int i=0;i<n;i++)
         {
             a[i]=L();
            }
         for(int i=1;i<n;i++)
         {
            if(a[i]<a[i-1])
            {
                c=c+a[i-1]-a[i];
                a[i]=a[i-1];
            }
            
            }
         pn(c);
        }
         public static void main(String[] args) throws IOException
  {
   try
{
        process();
 }
catch(Exception e)
{
return;
}
}
             
         
       }
   