#include <bits/stdc++.h>
int main(void) {
  int i, ans, saidai;
  long long int n;
  long long int a[1000000];
  ans = 0;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  saidai = a[0];
  for (i = 0; i < n; i++) {
    if (a[i] <= saidai) {
      while (a[i] < saidai) {
        a[i]++;
        ans++;
      }
    } else {
      saidai = a[i];
    }
  }
  printf("%d", ans);
  return 0;
}
