#include <bits/stdc++.h>
using std::pair;
using std::vector;
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  size_t n;
  std::cin >> n;
  vector<int64_t> data(n), t(n);
  for (auto& elem : data) {
    std::cin >> elem;
  }
  int64_t ans = 0, max = 0, cur = 0;
  for (int i = n - 1; i > -1; --i) {
    cur = std::max(int64_t(0), cur - 1);
    cur = std::max(cur, data[i] + 1);
    t[i] = cur;
  }
  for (int i = 0; i < n; ++i) {
    cur = std::max(cur, t[i]);
    ans += cur - data[i] - 1;
  }
  std::cout << ans;
  return 0;
}
