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