#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int N;
  cin >> N;
  int M[N];
  for (int i = 0; i < N; ++i) cin >> M[i];
  int C[N];
  C[N - 1] = M[N - 1] + 1;
  for (int i = N - 2; i >= 0; --i) C[i] = max(M[i] + 1, C[i + 1] - 1);
  int D[N];
  D[0] = 1;
  for (int i = 1; i < N; ++i) D[i] = max(D[i - 1], C[i]);
  long long ans = 0;
  for (int i = 0; i < N; ++i) ans += D[i] - M[i] - 1;
  cout << ans << '\n';
  return 0;
}
