import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
 
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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    }
} import java.io.*; io.* java import java.math.*; math.* java import java.security.*; security.* java import java.text.*; text.* java import java.util.*; util.* java import java.util.concurrent.*; concurrent.* java.util import java.util.function.*; function.* java.util import java.util.regex.*; regex.* java.util import java.util.stream.*; stream.* java.util import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList; 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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    }
} 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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    } 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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    } 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] a i in.nextLong() in.nextLong in long curr=a[0]; curr a[0] a 0 long total=0; total 0 for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        } {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        } if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            } a[i]>=curr a[i] a i curr curr=a[i] curr a[i] a i {
                total+=(curr-a[i]);
            } total+=(curr-a[i]) total (curr-a[i]) curr a[i] a i System.out.println(total) System.out.println System.out System System.out total String[] args args 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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    }
} 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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    }
} Main
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
 
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 curr=a[0];
        long total=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]>=curr)
                curr=a[i];
            else
            {
                total+=(curr-a[i]);
            }
        }
        System.out.println(total);

    }
}
