#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n;
  cin >> n;
  int a[n], mir[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  mir[0] = 0;
  for (int i = 1; i < n; i++) mir[i] = max(mir[i - 1], a[i]);
  for (int i = n - 2; i >= 0; i--) mir[i] = max(mir[i + 1] - 1, mir[i]);
  long long ans = 0;
  for (int i = 0; i < n; i++) ans += (mir[i] - a[i]);
  cout << ans << endl;
}
