#include <bits/stdc++.h>
using namespace std;
int n, m[200000], t[200000], maxx = 0, pos = 0;
long long ans = 0;
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> m[i];
  }
  t[0] = 1;
  for (int i = 1; i < n; ++i) {
    t[i] = max(t[i - 1], m[i] + 1);
    maxx = max(maxx, t[i]);
  }
  for (int i = 0; i < n; ++i) {
    if (t[i] == maxx) {
      pos = i;
      break;
    }
  }
  for (int i = pos - 1; i >= 0; --i) {
    t[i] = max(t[i + 1] - 1, t[i]);
  }
  for (int i = 0; i < n; ++i) {
    ans += max(0, t[i] - m[i] - 1);
  }
  cout << ans;
  return 0;
}
