import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  }
} import java.util.Scanner; Scanner java.util public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long fA = 0; fA 0 long ans = 0; ans 0 for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    } int cnt = 0; int cnt = 0; cnt 0 cnt < n cnt n cnt++ cnt++ cnt {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    } {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    } long a = Long.parseLong(sc.next()); a Long.parseLong(sc.next()) Long.parseLong Long sc.next() sc.next sc long dai = 0; dai 0 if (fA > a) {
        dai = (fA - a);
        ans += dai;
      } fA > a fA a {
        dai = (fA - a);
        ans += dai;
      } dai = (fA - a) dai (fA - a) fA a ans += dai ans dai fA = a + dai fA a + dai a dai System.out.println(ans + "") System.out.println System.out System System.out ans + "" ans "" sc.close() sc.close sc String[] args args public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  }
} Main
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long fA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      long a = Long.parseLong(sc.next());
      long dai = 0;
      if (fA > a) {
        dai = (fA - a);
        ans += dai;
      }
      fA = a + dai;
    }
    System.out.println(ans + "");
    sc.close();
  }
}