#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int a[100005];
int b[100005];
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  long long int ans = 0;
  for (int i = 1; i < N; i++) b[i] = max(b[i], b[i - 1]);
  for (int i = N - 2; i >= 0; i--) b[i] = max(b[i], b[i + 1] - 1);
  for (int i = 0; i < N; i++) ans += max(0, b[i] - a[i]);
  cout << ans << endl;
  return 0;
}
