#include <bits/stdc++.h>
using namespace std;
int main() {
  iostream::sync_with_stdio(false);
  int n;
  cin >> n;
  int* m = new int[n];
  for (int i = 0; i < n; i++) cin >> m[i];
  int* cnt = new int[n];
  for (int i = 0; i < n; i++) cnt[i] = m[i] + 1;
  for (int i = 1; i < n; i++) cnt[i] = max(cnt[i], cnt[i - 1]);
  for (int i = n - 2; i >= 0; i--) cnt[i] = max(cnt[i], cnt[i + 1] - 1);
  long long ans = 0;
  for (int i = 0; i < n; i++) ans += cnt[i] - m[i] - 1;
  cout << ans << '\n';
}
