import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main (String[] args) {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main {
	public static void main (String[] args) {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
}
} Main public static void main (String[] args) {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
} main {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
} Scanner in=new Scanner(System.in); in new Scanner(System.in) int n; n long sum=0; sum 0 n=in.nextInt() n in.nextInt() in.nextInt in int a[]=new int[n]; a new int[n] n for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      } int i=0; int i=0; i 0 i<n i n i++ i++ i {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      } {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      } a[i]=in.nextInt() a[i] a i in.nextInt() in.nextInt in if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        } i>0&&a[i]<a[i-1] i>0 i 0 a[i]<a[i-1] a[i] a i a[i-1] a i-1 i 1 {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        } sum=sum+(a[i-1]-a[i]) sum sum+(a[i-1]-a[i]) sum (a[i-1]-a[i]) 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 System.out.println(sum) System.out.println System.out System System.out sum String[] args args public class Main {
	public static void main (String[] args) {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
}
} public class Main {
	public static void main (String[] args) {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
}
} Main
import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
	public static void main (String[] args) {
		//code
		Scanner in=new Scanner(System.in);
		int n;
		long sum=0;
      n=in.nextInt();
      int a[]=new int[n];
      for(int i=0;i<n;i++)
      {
        a[i]=in.nextInt();
        if(i>0&&a[i]<a[i-1])
        {
          sum=sum+(a[i-1]-a[i]);
          a[i]=a[i-1];
        }
      }
      System.out.println(sum);
}
}
