#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T min(T a, T b, T c) {
  return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
  return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
const int mod = int(1e9) + 7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int dx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int N = 1e5 + 5;
int a[N], all[N];
int main() {
  ios::sync_with_stdio(false);
  int n;
  while (cin >> n) {
    long long ans = 0;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) {
      all[i] = max(all[i - 1], a[i] + 1);
    }
    for (int i = n - 2; i >= 0; --i) {
      all[i] = max(all[i], all[i + 1] - 1);
      ans += all[i] - a[i] - 1;
    }
    cout << ans + all[n - 1] - a[n - 1] - 1 << endl;
  }
  return 0;
}
