import java.util.*;
class Main{
	public static void main(String[] args){
	  Scanner scan = new Scanner(System.in);
	  int N  = scan.nextInt();
      int A = 0;
      long count = 0;
      int max = 0;
      for(int i = 0; i < N; i++){
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      }
      System.out.println(count);
	}
} import java.util.*; util.* java class Main{
	public static void main(String[] args){
	  Scanner scan = new Scanner(System.in);
	  int N  = scan.nextInt();
      int A = 0;
      long count = 0;
      int max = 0;
      for(int i = 0; i < N; i++){
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      }
      System.out.println(count);
	}
} Main public static void main(String[] args){
	  Scanner scan = new Scanner(System.in);
	  int N  = scan.nextInt();
      int A = 0;
      long count = 0;
      int max = 0;
      for(int i = 0; i < N; i++){
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      }
      System.out.println(count);
	} main {
	  Scanner scan = new Scanner(System.in);
	  int N  = scan.nextInt();
      int A = 0;
      long count = 0;
      int max = 0;
      for(int i = 0; i < N; i++){
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      }
      System.out.println(count);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int N  = scan.nextInt(); N scan.nextInt() scan.nextInt scan int A = 0; A 0 long count = 0; count 0 int max = 0; max 0 for(int i = 0; i < N; i++){
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      } int i = 0; int i = 0; i 0 i < N i N i++ i++ i {
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      } {
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      } A = scan.nextInt() A scan.nextInt() scan.nextInt scan if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          } i == 0 i 0 max = A max A {
             if(max <= A) max = A;
          	 count += max - A;
          } if(max <= A) max = A; max <= A max A max = A max A count += max - A count max - A max A System.out.println(count) System.out.println System.out System System.out count String[] args args
import java.util.*;
class Main{
	public static void main(String[] args){
	  Scanner scan = new Scanner(System.in);
	  int N  = scan.nextInt();
      int A = 0;
      long count = 0;
      int max = 0;
      for(int i = 0; i < N; i++){
          A = scan.nextInt();
          if(i == 0)    max = A;
          else{
             if(max <= A) max = A;
          	 count += max - A;
          }
      }
      System.out.println(count);
	}
}