#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
int n;
int above[MAXN];
int cnt[MAXN];
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) scanf("%d", &above[i]);
  for (int i = (0); i < (n); ++i) cnt[i] = above[i] + 1;
  for (int i = (1); i < (n); ++i) cnt[i] = max(cnt[i], cnt[i - 1]);
  for (int i = n - 2; i >= 0; --i) cnt[i] = max(cnt[i], cnt[i + 1] - 1);
  long long ret = 0;
  for (int i = (0); i < (n); ++i) ret += cnt[i] - above[i] - 1;
  printf("%lld\n", ret);
}
int main() {
  run();
  return 0;
}
