#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll DIM=100007;
const ll BDIM=1000000007;
const ll INF=2000000007;

long long n,t,k,num,mini,maxi,lastnum,mina,maxa,x,p,gp,entr,cost;
bool res;
long long b[DIM];

int main()
{
    cin >> n;
    vector<ll>v(n);

    for (int i=0;i<n;i++)
    {
        cin >> v[i];
        //b[v[i]]++;
    }

    reverse(v.begin(),v.end());

    p=0;
    gp=0;
    entr=0;
    cost=0;
    lastnum=0;
    for (int i=0;i<n;i++)
    {
        if (v[i]-p<1) break;
        if (lastnum==v[i])
        {
            cost+=v[i]-gp;
            p++;
        }
        else
        {
            if (entr>0) gp+=p;
            cost+=v[i]-gp;
            p++;
            lastnum=v[i];
            entr++;
        }
    }

    cout << cost << endl;

    return 0;
}
