#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
long long in[N], cnt[N];
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> in[i];
  long long mx = 0;
  for (int i = n; i >= 1; --i) {
    mx = max(in[i] + 1, mx - 1);
    cnt[i] = mx;
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    cnt[i] = max(cnt[i], cnt[i - 1]);
    ans += cnt[i] - in[i] - 1;
  }
  cout << ans << endl;
  return 0;
}
