#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; i++) cin >> b[i], a[i] = b[i];
  for (int i = 1; i < n; i++) a[i] = max(a[i], a[i - 1]);
  for (int i = n - 2; i >= 0; i--) a[i] = max(a[i], a[i + 1] - 1);
  long long ans = 0;
  for (int i = 0; i < n; i++) ans += abs(a[i] - b[i]);
  cout << ans << endl;
  return 0;
}
