#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
string s[55];
int n, i, j, k, l, a[101010], f[101010];
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    f[i] = ((a[i] + 1) > (f[i - 1]) ? (a[i] + 1) : (f[i - 1]));
  }
  for (i = n; i >= 1; i--) {
    f[i] = ((f[i + 1] - 1) > (f[i]) ? (f[i + 1] - 1) : (f[i]));
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += (long long)(f[i] - 1 - a[i]);
  }
  cout << ans;
}
