#include <bits/stdc++.h>
using namespace std;
long long int arr[100005], n, ans;
int main() {
  scanf("%lld", &n);
  for (long long int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
  for (long long int i = 1; i <= n; i++) {
    if (arr[i] < arr[i - 1]) ans += arr[i - 1] - arr[i], arr[i] = arr[i - 1];
  }
  for (long long int i = n; i >= 2; i--) {
    if (arr[i] - arr[i - 1] <= 1) continue;
    ans += arr[i] - arr[i - 1] - 1;
    arr[i - 1] = arr[i] - 1;
  }
  cout << ans << endl;
}
