import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  } main {
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) String line = sc.nextLine(); line sc.nextLine() sc.nextLine sc int n = Integer.parseInt(line); n Integer.parseInt(line) Integer.parseInt Integer line line = sc.nextLine() line sc.nextLine() sc.nextLine sc String HeiSt[] = new String[n]; HeiSt new String[n] n HeiSt = line.split(" ", 0) HeiSt line.split(" ", 0) line.split line " " 0 int hei0 = Integer.parseInt(HeiSt[0]); hei0 Integer.parseInt(HeiSt[0]) Integer.parseInt Integer HeiSt[0] HeiSt 0 int hei1; hei1 long step = 0; step 0 for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    } int i = 1; int i = 1; i 1 i < n i n i++ i++ i {
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    } {
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    } hei1 = Integer.parseInt(HeiSt[i]) hei1 Integer.parseInt(HeiSt[i]) Integer.parseInt Integer HeiSt[i] HeiSt i if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      } hei0 > hei1 hei0 hei1 {
        step += hei0 - hei1;
      } step += hei0 - hei1 step hei0 - hei1 hei0 hei1 if(hei0 < hei1){
        hei0 = hei1;
      } hei0 < hei1 hei0 hei1 {
        hei0 = hei1;
      } hei0 = hei1 hei0 hei1 System.out.println(step) System.out.println System.out System System.out step String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  }
} Main
import java.util.*;

public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    String line = sc.nextLine();
    int n = Integer.parseInt(line);
    
    line = sc.nextLine();
    String HeiSt[] = new String[n];
    HeiSt = line.split(" ", 0);
    int hei0 = Integer.parseInt(HeiSt[0]);
    int hei1;
    long step = 0;
    
    for(int i = 1; i < n; i++){
      hei1 = Integer.parseInt(HeiSt[i]);
      if(hei0 > hei1){
        step += hei0 - hei1;
      }else if(hei0 < hei1){
        hei0 = hei1;
      }
    }
    
    System.out.println(step);
  }
}