#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300500;
int fen[MAXN];
int arr[MAXN];
signed main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i] + 1;
  }
  for (int i = 1; i < n; i++) b[i] = max(b[i], b[i - 1]);
  for (int i = n - 2; i >= 0; i--) b[i] = max(b[i], b[i + 1] - 1);
  long long ans = 0;
  for (int i = 0; i < n; i++) ans += b[i] - a[i] - 1;
  cout << ans;
  return 0;
}
