#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> m;
  m.reserve(n);
  for (int i = 0; i < n; ++i) {
    int t;
    cin >> t;
    m.push_back(t);
  }
  vector<int> all;
  all.reserve(n + 1), all[0] = 0;
  all[1] = 1;
  for (int i = 1; i < n; ++i) {
    if (m[i] >= all[i])
      all[i + 1] = m[i] + 1;
    else
      all[i + 1] = all[i];
  }
  for (int i = n; i >= 2; --i) {
    if (all[i] - all[i - 1] > 1) all[i - 1] = all[i] - 1;
  }
  int64_t ans = 0;
  for (int i = 0; i < n; ++i)
    if (all[i] > m[i]) ans += (all[i + 1] - m[i] - 1);
  cout << ans;
  return 0;
}
