#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n,k=0,s=0;
    cin>>n;
    vector <ll> v;
    for (ll i=0;i<n;i++){
        ll a;
        cin>>a;
        v.push_back(a);
    }
    ll last=0,c=0;
    for (ll i=n-1;i>=0;i--){
        if (v[i]!=last){
            s+=max((ll)0,(v[i]-k));
            last=v[i];
            k++;
            c=0;
        } else {c++;s+=max((ll)0,v[i]-(k-c));k++;}
        //cout<<k<<" "<<s<<" "<<endl;
    }
    cout<<s<<endl;
}
