#include <bits/stdc++.h>
using namespace std;
template <typename T>
void _dbg(char const *s, const T &t) {
  cout << s << "=" << t << endl;
}
template <typename T, typename... TA>
void _dbg(char const *s, const T &t, const TA &...ta) {
  while (*s != ',') cout << *s++;
  cout << "=" << t << ",";
  _dbg(s + 1, ta...);
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << "{" << p.first << "," << p.second << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ",";
    os << *it;
  }
  return os << "}";
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const map<T1, T2> &mp) {
  os << "{";
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    if (it != mp.begin()) os << ",";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{";
  for (auto it = s.begin(); it != s.end(); ++it) {
    if (it != s.begin()) os << ",";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &s) {
  os << "{";
  for (auto it = s.begin(); it != s.end(); ++it) {
    if (it != s.begin()) os << ",";
    os << *it;
  }
  return os << "}";
}
void solve() {
  int n;
  scanf("%d", &n);
  vector<int> m(n);
  for (int &x : m) scanf("%d", &x);
  int p = max_element((m).begin(), (m).end()) - m.begin();
  ;
  vector<int> d = m;
  for (int i = p - 1; i >= 0; --i) {
    d[i] = max(m[i], d[i + 1] - 1);
  }
  for (int i = 1; i < n; ++i) {
    d[i] = max(d[i], d[i - 1]);
  };
  ;
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    res += d[i] - m[i];
  };
  cout << res << endl;
}
int main() {
  solve();
  return 0;
}
