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