#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int a[N + 10], c[N + 10];
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  long long ans = 0;
  cin >> n;
  cin >> a[0], c[0] = 1;
  for (int i = 1; i < n; i++) {
    cin >> a[i];
    c[i] = max(c[i - 1], a[i] + 1);
  }
  for (int i = n - 1; i > 0; i--) {
    if (c[i] > c[i - 1]) c[i - 1] = c[i] - 1;
    ans += c[i] - a[i] - 1;
  }
  cout << ans;
  return 0;
}
