#include <bits/stdc++.h>
using namespace std;
const int N = 100050, INF = 0x3f3f3f3f;
struct Node {
  int id;
  long long value;
};
bool cmp(Node a, Node b) {
  if (a.value == b.value) a.id < b.id;
  return a.value > b.value;
}
Node tr[N];
long long sum[N];
long long a[N];
int main() {
  int n, r;
  long long maxs = 0, ans = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    tr[i].value = a[i];
    tr[i].id = i;
  }
  sort(tr, tr + n, cmp);
  for (int i = n - 1; i >= 0; i--) sum[i] = sum[i + 1] + a[i] + 1;
  maxs = tr[0].value;
  r = n;
  for (int i = 0; i < n; i++) {
    if (r < tr[i].id || maxs - tr[i].value > r - tr[i].id) continue;
    ans += (tr[i].value + 1) * (r - tr[i].id) - sum[tr[i].id] + sum[r] +
           (maxs - tr[i].value) * (maxs - tr[i].value - 1) / 2;
    maxs = tr[i].value;
    r = tr[i].id;
  }
  printf("%lld\n", ans);
  return 0;
}
