import java.util.*;
 
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  } main {
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int num = sc.nextInt(); num sc.nextInt() sc.nextInt sc int array[] = new int[num]; array new int[num] num for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    } int i = 0; int i = 0; i 0 i < num i num i++ i++ i {
      array[i] = sc.nextInt();
    } {
      array[i] = sc.nextInt();
    } array[i] = sc.nextInt() array[i] array i sc.nextInt() sc.nextInt sc long count = 0; count 0 int max = 0; max 0 for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    } int i = 0; int i = 0; i 0 i < num i num i++ i++ i {
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    } {
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    } if(max < array[i]){
        max = array[i];
      } max < array[i] max array[i] array i {
        max = array[i];
      } max = array[i] max array[i] array i if(array[i] < max){
        count += max - array[i];
      } array[i] < max array[i] array i max {
        count += max - array[i];
      } count += max - array[i] count max - array[i] max array[i] array i System.out.println(count) System.out.println System.out System System.out count String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  }
} Main
import java.util.*;
 
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int array[] = new int[num];
    for(int i = 0;i < num;i++){
      array[i] = sc.nextInt();
    }
    long count = 0;
    int max = 0;
    for(int i = 0;i < num;i++){
      if(max < array[i]){
        max = array[i];
      }
      if(array[i] < max){
        count += max - array[i];
      }
    }
    System.out.println(count);
  }
}