#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
int m[MAXN], mark[MAXN];
int main() {
  int n;
  scanf("%d", &n);
  int id = 0, mx = 0;
  ;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m[i]);
    if (mx < m[i] + 1) {
      mx = m[i] + 1;
      id = i;
    }
  }
  long long ans = 0;
  for (int i = id + 1; i <= n; i++) ans += mx - m[i] - 1;
  for (int i = 1; i < id; i++) mark[i] = max(mark[i - 1], m[i] + 1);
  int now = mx;
  for (int i = id - 1; i; i--) {
    if (now > mark[i]) now--;
    ans += now - m[i] - 1;
  }
  printf("%lld\n", ans);
}
