#include <bits/stdc++.h>
using namespace std;
int n, cnt[200005], mx[200005];
long long sum;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> mx[i];
  for (int i = 1; i <= n; i++) cnt[i] = max(mx[i] + 1, cnt[i - 1]);
  for (int j = n - 1; j >= 1; j--) cnt[j] = max(cnt[j], cnt[j + 1] - 1);
  for (int i = 1; i <= n; i++) sum -= (mx[i] + 1), sum += cnt[i];
  cout << sum;
  return 0;
}
