import java.util.*;
public class Main
{
  public static void main(String[] args)
  {
   Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  }
} import java.util.*; util.* java public class Main
{
  public static void main(String[] args)
  {
   Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  }
} Main public static void main(String[] args)
  {
   Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  } main {
   Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  } Scanner s=new Scanner(System.in); s new Scanner(System.in) int n=s.nextInt(); n s.nextInt() s.nextInt s int a[]=new int[n]; a new int[n] n for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      a[i]=s.nextInt();
    } {
      a[i]=s.nextInt();
    } a[i]=s.nextInt() a[i] a i s.nextInt() s.nextInt s long ans=0; ans 0 for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    } int i=1; int i=1; i 1 i<n i n i++ i++ i {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    } {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    } ans+=Math.max(0,a[i-1]-a[i]) ans Math.max(0,a[i-1]-a[i]) Math.max Math 0 a[i-1]-a[i] a[i-1] a i-1 i 1 a[i] a i a[i]=Math.max(a[i],a[i-1]) a[i] a i Math.max(a[i],a[i-1]) Math.max Math a[i] a i a[i-1] a i-1 i 1 System.out.println(ans) System.out.println 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();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  }
} public class Main
{
  public static void main(String[] args)
  {
   Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  }
} Main
import java.util.*;
public class Main
{
  public static void main(String[] args)
  {
   Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    int a[]=new int[n];
    for(int i=0;i<n;i++)
    {
      a[i]=s.nextInt();
    }
    long ans=0;
    for(int i=1;i<n;i++)
    {
      ans+=Math.max(0,a[i-1]-a[i]);
      a[i]=Math.max(a[i],a[i-1]);
    }
    System.out.println(ans);
  }
}