#include <bits/stdc++.h>
using namespace std;
const int max_n = 100000 + 10;
const int inf = 0x3f3f3f3f;
int n;
int above[max_n];
long long below[max_n];
int cnt, head[max_n];
bool in[max_n];
struct ini {
  int to, next;
  long long len;
  ini() {}
  ini(int to, long long len, int next) : to(to), len(len), next(next) {}
} edge[max_n << 2];
inline void addedge(int from, int to, long long len) {
  edge[cnt] = ini(to, len, head[from]);
  head[from] = cnt++;
}
void spfa() {
  queue<int> que;
  que.push(0);
  in[0] = true;
  below[0] = 0;
  while (que.size()) {
    int u = que.front();
    que.pop();
    in[u] = false;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      if (below[v] < below[u] + edge[i].len) {
        below[v] = below[u] + edge[i].len;
        if (!in[v]) {
          in[v] = true;
          que.push(v);
        }
      }
    }
  }
}
int main() {
  memset(head, -1, sizeof(head));
  memset(in, false, sizeof(in));
  memset(below, -1, sizeof(below));
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &above[i]);
    addedge(0, i, 0);
  }
  for (int i = 1; i <= n; ++i) {
    addedge(i - 1, i, above[i - 1] - above[i]);
    addedge(i, i - 1, above[i] - above[i - 1] - 1);
  }
  spfa();
  long long ans = 0;
  for (int i = 1; i <= n; ++i) ans += below[i];
  printf("%I64d\n", ans);
  return 0;
}
