#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n, a[N + 9];
void into() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
}
int c[N + 9];
long long ans;
void Get_ans() {
  for (int i = 1; i <= n; ++i) c[i] = max(c[i - 1], a[i] + 1);
  for (int i = n; i >= 1; --i) c[i] = max(c[i], c[i + 1] - 1);
  for (int i = 1; i <= n; ++i) ans += c[i] - a[i] - 1;
}
void work() { Get_ans(); }
void outo() { printf("%lld\n", ans); }
int main() {
  int T = 1;
  for (; T--;) {
    into();
    work();
    outo();
  }
  return 0;
}
