import java.util.*;
 
class Main {
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    //人数
    int n = sc.nextInt();
    //身長
    long[] h = new long[n];
    
    for(int i = 0;i < n;i++){
      h[i] = sc.nextLong();
    }
    
    
    
    long[] step = new long[n];
    for(int i = 0;i < n;i++){
      step[i] = 0;
    }
    long ans = 0;
    for(int i = 1;i < n;i++){
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    }
    
    for(int i = 0;i < n;i++){
      ans += step[i];
    }
    System.out.print(ans);
  }
} import java.util.*; util.* java class Main {
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    //人数
    int n = sc.nextInt();
    //身長
    long[] h = new long[n];
    
    for(int i = 0;i < n;i++){
      h[i] = sc.nextLong();
    }
    
    
    
    long[] step = new long[n];
    for(int i = 0;i < n;i++){
      step[i] = 0;
    }
    long ans = 0;
    for(int i = 1;i < n;i++){
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    }
    
    for(int i = 0;i < n;i++){
      ans += step[i];
    }
    System.out.print(ans);
  }
} Main public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    //人数
    int n = sc.nextInt();
    //身長
    long[] h = new long[n];
    
    for(int i = 0;i < n;i++){
      h[i] = sc.nextLong();
    }
    
    
    
    long[] step = new long[n];
    for(int i = 0;i < n;i++){
      step[i] = 0;
    }
    long ans = 0;
    for(int i = 1;i < n;i++){
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    }
    
    for(int i = 0;i < n;i++){
      ans += step[i];
    }
    System.out.print(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    //人数
    int n = sc.nextInt();
    //身長
    long[] h = new long[n];
    
    for(int i = 0;i < n;i++){
      h[i] = sc.nextLong();
    }
    
    
    
    long[] step = new long[n];
    for(int i = 0;i < n;i++){
      step[i] = 0;
    }
    long ans = 0;
    for(int i = 1;i < n;i++){
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    }
    
    for(int i = 0;i < n;i++){
      ans += step[i];
    }
    System.out.print(ans);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] h = new long[n]; h new long[n] n for(int i = 0;i < n;i++){
      h[i] = sc.nextLong();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      h[i] = sc.nextLong();
    } {
      h[i] = sc.nextLong();
    } h[i] = sc.nextLong() h[i] h i sc.nextLong() sc.nextLong sc long[] step = new long[n]; step new long[n] n for(int i = 0;i < n;i++){
      step[i] = 0;
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      step[i] = 0;
    } {
      step[i] = 0;
    } step[i] = 0 step[i] step i 0 long ans = 0; ans 0 for(int i = 1;i < n;i++){
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    } {
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    } if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      } h[i - 1] > h[i] h[i - 1] h i - 1 i 1 h[i] h i {
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      } step[i] = h[i - 1] - h[i] step[i] step i h[i - 1] - h[i] h[i - 1] h i - 1 i 1 h[i] h i h[i] += step[i] h[i] h i step[i] step i for(int i = 0;i < n;i++){
      ans += step[i];
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      ans += step[i];
    } {
      ans += step[i];
    } ans += step[i] ans step[i] step i System.out.print(ans) System.out.print System.out System System.out ans String args[] args
import java.util.*;
 
class Main {
  public static void main(String args[]) {
    Scanner sc = new Scanner(System.in);
    //人数
    int n = sc.nextInt();
    //身長
    long[] h = new long[n];
    
    for(int i = 0;i < n;i++){
      h[i] = sc.nextLong();
    }
    
    
    
    long[] step = new long[n];
    for(int i = 0;i < n;i++){
      step[i] = 0;
    }
    long ans = 0;
    for(int i = 1;i < n;i++){
      if(h[i - 1] > h[i]){
        step[i] = h[i - 1] - h[i];
        h[i] += step[i];
      }
    }
    
    for(int i = 0;i < n;i++){
      ans += step[i];
    }
    System.out.print(ans);
  }
}