import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  }
  
} import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  }
  
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  } main {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = Integer.parseInt(sc.next()); n Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc int[] a = new int[n]; a new int[n] n for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    } int i=0; int i=0; i 0 i<n i n i++ i++ i {
      a[i] = Integer.parseInt(sc.next());
    } {
      a[i] = Integer.parseInt(sc.next());
    } a[i] = Integer.parseInt(sc.next()) a[i] a i Integer.parseInt(sc.next()) Integer.parseInt Integer sc.next() sc.next sc long sum = 0; sum 0 for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    } int i = 1; int i = 1; i 1 i<n i n i++ i++ i {
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    } {
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    } int dif = a[i-1] - a[i]; dif a[i-1] - a[i] a[i-1] a i-1 i 1 a[i] a i if(dif > 0){
        sum += dif;
        a[i] += dif;
      } dif > 0 dif 0 {
        sum += dif;
        a[i] += dif;
      } sum += dif sum dif a[i] += dif a[i] a i dif System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc return; String[] args args public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  }
  
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  }
  
} Main
import java.util.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.next());
    int[] a = new int[n];
    for(int i=0; i<n; i++){
      a[i] = Integer.parseInt(sc.next());
    }

    long sum = 0;
    
    for(int i = 1; i<n; i++){
      int dif = a[i-1] - a[i];
      if(dif > 0){
        sum += dif;
        a[i] += dif;
      }
    }

    System.out.println(sum);
   
    sc.close();
    return;
  }
  
}
