import java.util.*;

public class Main
{
  public static void main(String[] args)
  {  	
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  }
} import java.util.*; util.* java public class Main
{
  public static void main(String[] args)
  {  	
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  }
} Main public static void main(String[] args)
  {  	
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  } main {  	
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long total = 0; total 0 int p = sc.nextInt(); p sc.nextInt() sc.nextInt sc for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    } int i = 1; int i = 1; i 1 i<n i n i++ i++ i {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    } {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    } int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc if (p - c > 0)
        {
            total += p - c;
            c = p;
        } p - c > 0 p - c p c 0 {
            total += p - c;
            c = p;
        } total += p - c total p - c p c c = p c p p = c p c 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 sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  }
} public class Main
{
  public static void main(String[] args)
  {  	
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  }
} Main
import java.util.*;

public class Main
{
  public static void main(String[] args)
  {  	
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    // int[] arr = new int[n];

    long total = 0;

    int p = sc.nextInt();
    for (int i = 1; i<n; i++)
    {
        int c = sc.nextInt();
        if (p - c > 0)
        {
            total += p - c;
            c = p;
        }
        p = c;
    }

    System.out.println(total);
  }
}