#include <bits/stdc++.h>
using namespace std;
const int Z = (int)1e5 + 111;
const int INF = (int)1e9 + 111;
const int MODF = (int)1e9 + 7;
int n, m[Z];
vector<pair<int, int> > v;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
    if (!v.size() || v.back().first < m[i]) v.push_back({m[i], i});
  }
  long long ans = 0;
  long long deapth = v.back().first;
  for (int i = v.size() - 1; i > 0; i--) {
    for (int j = v[i].second - 1; j >= v[i - 1].second; j--) {
      if (deapth > v[i - 1].first) deapth--;
      ans += deapth - m[j];
    }
  }
  for (int i = v.back().second; i < n; i++) {
    ans += v.back().first - m[i];
  }
  cout << ans;
  return 0;
}
