#include <bits/stdc++.h>
using namespace std;
template <class T>
ostream& operator<<(ostream& os, vector<T> v) {
  for (long long i = (0); i < ((long long)(v).size()); i++)
    os << (i ? " " : "") << v[i];
  return os;
}
const bool debug = 1;
vector<long long> tab;
long long maxi[100001];
long long sum[100002];
signed main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    tab.push_back(a);
    if (i == 0)
      maxi[i] = 1;
    else
      maxi[i] = max(maxi[i - 1], a + 1);
    sum[i + 1] = sum[i] + a;
  }
  for (long long j = n - 1; j > 0; j--) {
    maxi[j - 1] = max(maxi[j - 1], maxi[j] - 1);
  }
  long long m = maxi[n - 1];
  long long ans = 0;
  long long prev = n;
  for (long long i = m; i > 0; i--) {
    long long l = 0, u = prev;
    while (l < u) {
      long long mid = (l + u) / 2;
      if (maxi[mid] >= i) {
        u = mid;
      } else {
        l = mid + 1;
      }
    }
    ans += (i - 1) * (prev - l) - (sum[prev] - sum[l]);
    prev = l;
  }
  cout << ans << endl;
  return 0;
}
