#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cout.precision(30);
  cout << boolalpha << fixed;
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> b(n);
  int x = 0;
  for (int i = n - 1; i >= 0; i--) {
    x = b[i] = max(x, a[i] + 1);
    x--;
  }
  for (int i = 1; i < n; i++) {
    b[i] = max(b[i], b[i - 1]);
  }
  ll result = 0;
  for (int i = 1; i < n; i++) {
    result += b[i] - a[i] - 1;
  }
  cout << result << endl;
  return 0;
}
