import java.util.*;
public class Main
{
  public static Scanner sc = new Scanner(System.in);
  public static void main(String[]args)
  {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  }
} import java.util.*; util.* java public class Main
{
  public static Scanner sc = new Scanner(System.in);
  public static void main(String[]args)
  {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  }
} Main public static Scanner sc = new Scanner(System.in); sc new Scanner(System.in) public static void main(String[]args)
  {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  } main {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  } int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int[]height = new int[n]; height new int[n] n for(int i=0;i<n;i++)
      height[i]=sc.nextInt(); int i=0; int i=0; i 0 i<n i n i++ i++ i height[i]=sc.nextInt(); height[i]=sc.nextInt() height[i] height i sc.nextInt() sc.nextInt sc int max = height[0]; max height[0] height 0 long total =0; total 0 for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    } int i=1; int i=1; i 1 i<n i n i++ i++ i {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    } {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    } if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      } height[i]==max height[i] height i max continue; if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      } height[i]<max height[i] height i max total+=max-height[i] total max-height[i] max height[i] height i {
        max = height[i];
      } max = height[i] max height[i] height i System.out.println(total) System.out.println System.out System System.out total String[]args args public class Main
{
  public static Scanner sc = new Scanner(System.in);
  public static void main(String[]args)
  {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  }
} public class Main
{
  public static Scanner sc = new Scanner(System.in);
  public static void main(String[]args)
  {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  }
} Main
import java.util.*;
public class Main
{
  public static Scanner sc = new Scanner(System.in);
  public static void main(String[]args)
  {
    int n = sc.nextInt();
    int[]height = new int[n];
    for(int i=0;i<n;i++)
      height[i]=sc.nextInt();
    int max = height[0];
    long total =0;
    for(int i=1;i<n;i++)
    {
      if(height[i]==max)
        continue;
      else if(height[i]<max)
        total+=max-height[i];
      else
      {
        max = height[i];
      }
    }
    System.out.println(total);
  }
}
