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