#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int N;
  cin >> N;
  vector<int> MU(N + 1), M(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> MU[i];
    M[i] = max(MU[i], M[i - 1]);
  }
  for (int j = N - 1; j >= 1; j--) {
    if (M[j] < M[j + 1] - 1) M[j] = M[j + 1] - 1;
  }
  long long sol = 0;
  for (int i = 1; i <= N; i++) {
    sol += M[i] - MU[i];
  }
  cout << sol << '\n';
  return 0;
}
