#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int a[N], p[N];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = n; i >= 1; i--) p[i] = max(p[i + 1] - 1, a[i]);
  for (int i = 1; i <= n; i++) p[i] = max(p[i - 1], p[i]);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    long long val = p[i] - a[i];
    sum += val;
  }
  cout << sum << endl;
}
