#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const long long inf64 = 1e18;
const int N = 1e5 + 5;
int n;
int m[N];
int a[N];
int main() {
  scanf("%d", &n);
  long long res = 0;
  int last = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m[i]);
    a[i] = a[i - 1];
    a[i] = max(a[i], m[i] + 1);
  }
  for (int i = n - 1; i >= 1; i--) {
    if (a[i] + 1 < a[i + 1]) {
      a[i] = a[i + 1] - 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    res += a[i] - m[i] - 1;
  }
  cout << res << "\n";
  return 0;
}
