#include <bits/stdc++.h>
using namespace std;
int n;
int a[100100], b[100100];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  b[n - 1] = a[n - 1] + 1;
  for (int i = n - 2; i >= 0; i--) b[i] = max(b[i + 1] - 1, a[i] + 1);
  long long ans = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt = max(cnt, b[i]);
    ans += cnt - a[i] - 1;
  }
  cout << ans << endl;
  return 0;
}
