#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long R = 7 + 1e9, NUMTESTCASE;
const long long NN = 10 + 1e6;
const double pi = acos(-1.0);
int di[8] = {1, 0, -1, 0, 1, -1, 1, -1}, dj[8] = {0, 1, 0, -1, 1, -1, -1, 1};
int Arr[NN], Brr[NN];
int main() {
  int n;
  cin >> n;
  long long Ans = 0;
  for (int i = (1); i <= (n); ++i)
    scanf("%d", Arr + i), Brr[i] = max(Arr[i], Brr[i - 1]);
  for (int i = n; i >= 1; i--) Brr[i] = max(Brr[i], Brr[i + 1] - 1);
  for (int i = (1); i <= (n); ++i) Ans += max(0, Brr[i] - Arr[i]);
  cout << Ans;
  return 0;
}
