#include <bits/stdc++.h>
using namespace std;
long long a[100005], b[100005];
int main() {
  long long n, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  b[0] = 1;
  for (int i = 1; i <= n; i++) {
    b[i] = max(b[i - 1], a[i] + 1);
  }
  for (int i = n; i >= 2; i--) {
    if (b[i] == b[i - 1]) continue;
    b[i - 1] = max(b[i] - 1, b[i - 1]);
  }
  for (int i = 1; i <= n; i++) {
    ans += b[i] - a[i] - 1;
  }
  cout << ans << endl;
}
