#include <bits/stdc++.h>
int main() {
  int n;
  int i, j, k;
  scanf("%d", &n);
  int x[n], count = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }
  for (i = 0; i < n - 1; i++) {
    while (x[i] < x[i + 1]) {
      x[i]++;
      count++;
    }
  }
  printf("%d\n", count);
  return 0;
}
