#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 5;
const double INF = 0x3f3f3f3f;
const double EPS = 1e-6;
long long n, m[MAX_N], mx, num[MAX_N], ans;
void solve() {
  mx = -1;
  scanf("%I64d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%I64d", m + i);
    if (m[i] > mx) {
      mx = m[i];
    }
    num[i] = mx + 1;
  }
  for (int i = n - 1; i >= 1; --i) {
    if (num[i + 1] - num[i] > 1) {
      num[i] = num[i + 1] - 1;
    }
  }
  for (int i = 1; i <= n; ++i) {
    ans += num[i] - m[i] - 1;
  }
  printf("%I64d", ans);
}
int main() {
  solve();
  return 0;
}
