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