#include <bits/stdc++.h>
using namespace std;
class heap_cmp {
 public:
  bool operator()(pair<int, int> &a, pair<int, int> &b) const {
    return a.first != b.first ? (a.first < b.first) : (a.second >= b.second);
  }
};
int64_t calc_sum(vector<int>::iterator start, vector<int>::iterator end,
                 int n_marks) {
  int64_t result = 0;
  for (auto ptr = start; ptr < end; ++ptr) {
    result += n_marks - *ptr;
  }
  return result;
}
int64_t run(vector<int> &ary) {
  int n = ary.size();
  int n_marks = 0;
  int64_t ans = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>, heap_cmp> pqueue;
  for (int i = 0; i < n; ++i) {
    n_marks = max(n_marks, ary[i]);
    pqueue.push({ary[i], i});
  }
  int ed = n;
  for (; n_marks > 0; --n_marks) {
    for (; !pqueue.empty() &&
           (ed <= pqueue.top().second || n_marks < pqueue.top().first);
         pqueue.pop()) {
      continue;
    }
    if (pqueue.empty()) break;
    auto cur = pqueue.top();
    if (n_marks == cur.first) {
      ans += calc_sum(ary.begin() + cur.second, ary.begin() + ed, n_marks);
      ed = cur.second;
    } else {
      ans += calc_sum(ary.begin() + (ed - 1), ary.begin() + ed, n_marks);
      ed--;
    }
  }
  return ans;
}
char input(vector<int> &ary) {
  int n, p;
  int rc = scanf("%d", &n);
  ary = vector<int>();
  if (rc != EOF) {
    for (int i = 0; i < n; ++i) {
      scanf("%d", &p);
      ary.push_back(p);
    }
  }
  return rc;
}
int main() {
  vector<int> ary;
  while (input(ary) > 0) {
    printf("%" PRId64 "\n", run(ary));
  }
  return 0;
}
