#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
	int n, k = 1, f = 0;
    long long ans = 0;
	cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    ans += arr[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        if (arr[i] == arr[i + 1]) {
            ans += arr[i] - f;
            k++;
        }
        else {
            f = k;
            if (f >= arr[i]) {
                break;
            }
            ans += arr[i] - f;
            k++;
        }
    }
    cout << ans;
}
