#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int n, h[MAXN], rh[MAXN];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &h[i]);
  for (int i = 1; i <= n; ++i) rh[i] = max(rh[i - 1], h[i] + 1);
  for (int i = n - 1; i; --i) rh[i] = max(rh[i], rh[i + 1] - 1);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) ans += rh[i] - 1 - h[i];
  printf("%lld\n", ans);
  return 0;
}
