#include <bits/stdc++.h>
using namespace std;
int t[100005];
int a[100005];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  int o = 0;
  for (int i = n; i >= 1; --i) {
    o = max(o - 1, a[i] + 1);
    t[i] = o;
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    t[i] = max(t[i], t[i - 1]);
    ans += (long long)t[i] - a[i] - 1;
  }
  cout << ans;
}
int main() { solve(); }
