import java.util.Scanner;
import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int beforeA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    }
    System.out.println(ans);
    sc.close();
  }
} import java.util.Scanner; Scanner java.util import java.util.Arrays; Arrays java.util public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int beforeA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    }
    System.out.println(ans);
    sc.close();
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int beforeA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    }
    System.out.println(ans);
    sc.close();
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int beforeA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    }
    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 int beforeA = 0; beforeA 0 long ans = 0; ans 0 for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    } int cnt = 0; int cnt = 0; cnt 0 cnt < n cnt n cnt++ cnt++ cnt {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    } {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    } int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int step = 0; step 0 if (beforeA > a) {
        step = beforeA - a;
      } beforeA > a beforeA a {
        step = beforeA - a;
      } step = beforeA - a step beforeA - a beforeA a ans += step ans step beforeA = a + step beforeA a + step a step System.out.println(ans) System.out.println System.out System System.out 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();
    int beforeA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    }
    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();
    int beforeA = 0;
    long ans = 0;
    for (int cnt = 0; cnt < n; cnt++) {
      int a = sc.nextInt();
      int step = 0;
      if (beforeA > a) {
        step = beforeA - a;
      }
      ans += step;
      beforeA = a + step;
    }
    System.out.println(ans);
    sc.close();
  }
} Main
import java.util.Scanner;
import java.util.Arrays;

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