#include <bits/stdc++.h>
using namespace std;
struct str {
  long long x, y, z, w;
  bool operator<(const str& A) const { return w < A.w; }
};
int arr[300010];
int n, m, k, v, U;
vector<int> vec;
string s;
vector<int> vec2;
long long chk[100010];
int cnt = 0;
int h[100010];
int main() {
  scanf("%d", &n);
  int mx = 0;
  long long ans = 0;
  int idx = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
    if (mx < arr[i]) {
      mx = arr[i];
      idx = i;
    }
    h[i] = mx + 1;
  }
  mx++;
  bool trig = false;
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] + 1 == mx && idx == i) {
      mx--;
      trig = true;
    } else {
      if (mx > h[i] && !trig) {
        mx--;
      }
      ans += (mx - arr[i] - 1);
      trig = false;
    }
  }
  printf("%lld", ans);
  return 0;
}
