#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int a[n+1];
    a[n] = -1;
    int str = 0;
    int s = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    int res = 0;
    for(int i = n-1; i >= 0; i--){
        if(a[i] != a[i+1]){
            str = 0;
            if(a[i] - s >= 1){
                res += a[i]-s;
            }else{
                cout << res;
                return 0;
            }
        }else{
            str++;
            res += a[i]-s+str;
        }
        s++;
    }
    cout << res;
}
