#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template <typename T>
using vc = std::vector<T>;
void solve(istream& cin, ostream& cout) {
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = (0); i < int(n); ++i) {
    cin >> a[i];
    b[i] = a[i] + 1;
  }
  for (int i = int(n - 1) - 1; i >= (0); --i) {
    b[i] = max(b[i], b[i + 1] - 1);
  }
  for (int i = (1); i < int(n); ++i) {
    b[i] = max(b[i], b[i - 1]);
  }
  lint ans = 0;
  int c = 0;
  for (int i = (0); i < int(n); ++i) {
    if (c < b[i]) {
      c++;
      assert(c == b[i]);
    }
    ans += c - a[i] - 1;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve(cin, cout);
  return 0;
}
