#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long arr[200005], i, n, k, st, maxi, j, temp[200005], perm[200005],
      ans = 0;
  cin >> n;
  for (i = 0; i < n; i++) cin >> arr[i];
  maxi = 0;
  for (j = n - 1; j >= 0; j--) {
    if (arr[j] >= maxi) {
      maxi = arr[j];
      st = j;
    }
  }
  k = arr[st] + 1;
  for (i = st; i >= 0; i--) {
    temp[i] = k;
    k--;
  }
  k = arr[st] + 1;
  for (i = st + 1; i < n; i++) perm[i] = k;
  perm[0] = 1;
  for (i = 1; i <= st; i++) {
    perm[i] = max(perm[i - 1], arr[i] + 1);
    perm[i] = max(perm[i], temp[i]);
  }
  for (j = n - 1; j >= 1; j--) {
    if (perm[j] - perm[j - 1] > 1) perm[j - 1] = perm[j] - 1;
  }
  for (j = 0; j < n; j++) {
    ans = ans + perm[j] - 1 - arr[j];
  }
  cout << ans << endl;
  return 0;
}
