#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  long long int a[n], left[n];
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (i == 0) {
      left[i] = a[i];
    } else {
      left[i] = max(left[i - 1], a[i]);
    }
  }
  int mx = left[n - 1];
  for (long long int i = n - 1; i >= 0; i--) {
    if (mx != left[i]) {
      mx--;
    }
    ans += mx - a[i];
  }
  cout << ans;
}
