#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const int infLL = 1ll << 62;
const int mod1 = 1000000007;
const int mod2 = 1000000009;
int n;
int m[1 << 20];
int diff_nums[1 << 20];
long long AnswerProblem;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    diff_nums[i] = m[i];
    diff_nums[i]++;
    diff_nums[i] = max(diff_nums[i], diff_nums[i + 1] - 1);
  }
  for (int i = 1; i < n; i++) {
    diff_nums[i] = max(diff_nums[i], diff_nums[i - 1]);
  }
  for (int i = 0; i < n; i++) {
    AnswerProblem += diff_nums[i] - m[i] - 1;
  }
  cout << AnswerProblem << endl;
  return 0;
}
