#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    v[i] = x;
  }
  int t[n];
  t[n - 1] = v[n - 1] + 1;
  for (int i = n - 1; i > 0; i--) {
    t[i - 1] = max(t[i] - 1, (v[i - 1] + 1));
  }
  for (int i = 1; i < n; i++) {
    t[i] = max(t[i - 1], t[i]);
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += (1ll) * (t[i] - (v[i] + 1));
  }
  cout << ans << endl;
  return 0;
}
