#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n;
  cin >> n;
  long long int a[n];
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int ans = 0;
  long long int x = LLONG_MAX;
  for (long long int i = n - 1; i >= 0; i--) {
    if (a[i] == 0) break;
    x = min(x - 1, a[i]);
    x = max((long long int)0, x);
    ans += x;
  }
  cout << ans << "\n";
}
