#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, m_i, j, sum = 0, propagate;
  cin >> n;
  vector<long long int> min_t, d_i;
  cin >> m_i;
  min_t.push_back(++m_i);
  d_i.push_back(min_t[0] - m_i);
  for (i = 1; i < n; i++) {
    cin >> m_i;
    m_i++;
    if ((m_i - min_t[i - 1]) > 1) {
      propagate = m_i - min_t[i - 1] - 1;
      for (j = 0; j < propagate; j++) {
        min_t[i - j - 1] += propagate - j;
        sum += propagate - j;
      }
    }
    min_t.push_back(max(min_t[i - 1], m_i));
    sum += min_t[i] - m_i;
  }
  while (--n > 0) {
    if ((min_t[n] - min_t[n - 1]) > 1) {
      sum += (min_t[n] - min_t[n - 1]) - 1;
      min_t[n - 1] += (min_t[n] - min_t[n - 1]) - 1;
    }
  }
  cout << sum;
  return 0;
}
