#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
int a[N], mx[N], v[N];
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  for (int i = 1; i <= n; ++i) mx[i] = max(mx[i - 1], a[i]);
  for (int i = n; i >= 1; --i) v[i] = max(1 + mx[i], v[i + 1] - 1);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += v[i] - a[i] - 1;
  }
  cout << ans << '\n';
}
