import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  }
} import java.util.*; util.* java public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  }
} Main public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc long[] heights = new long[n]; heights new long[n] n for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      heights[i] = sc.nextLong();
    } {
      heights[i] = sc.nextLong();
    } heights[i] = sc.nextLong() heights[i] heights i sc.nextLong() sc.nextLong sc long ans = 0; ans 0 for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    } int j = 0; int j = 0; j 0 j < n - 1 j n - 1 n 1 j++ j++ j {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    } {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    } if (heights[j] <= heights[j + 1]) continue; heights[j] <= heights[j + 1] heights[j] heights j heights[j + 1] heights j + 1 j 1 continue; ans += heights[j] - heights[j + 1] ans heights[j] - heights[j + 1] heights[j] heights j heights[j + 1] heights j + 1 j 1 heights[j + 1] = heights[j] heights[j + 1] heights j + 1 j 1 heights[j] heights j System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  }
} public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  }
} Main
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] heights = new long[n];
    for (int i = 0; i < n; i++) {
      heights[i] = sc.nextLong();
    }
    long ans = 0;
    for (int j = 0; j < n - 1; j++) {
      if (heights[j] <= heights[j + 1]) continue;
      ans += heights[j] - heights[j + 1];
      heights[j + 1] = heights[j];
    }
    System.out.println(ans);
  }
}
