#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int maxx = 1e9;
long long n, m[maxn], sum[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> m[i];
  long long ans = 0;
  sum[1] = 1;
  for (int i = 2; i <= n; i++) {
    sum[i] = sum[i - 1];
    if (m[i] < sum[i])
      ans += sum[i] - m[i] - 1;
    else {
      sum[i] = m[i] + 1;
    }
  }
  for (int i = n; i >= 2; i--) {
    if (sum[i] - sum[i - 1] <= 1) continue;
    ans += sum[i] - sum[i - 1] - 1;
    sum[i - 1] = sum[i] - 1;
  }
  cout << ans << endl;
  return 0;
}
