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