#include <iostream>
#include <bits/stdc++.h>
#include <vector>

using namespace std;

int main()
{
    int n,summ,k,step;
    vector <int> a;
    cin >> n;
    a.resize(n);
    summ=0;
    k=0;
    step=1;
    a[n]=1;
    for (int i=0; i<n; ++i)
    {
        cin >> a[i];
    }
    for (int i=n-1; i>=0; --i)
    {
        if (a[i]==a[i+1])
        {
            if (a[i]-k+step<=0)
            break;
            summ=summ+a[i]-k+step;
            ++step;
        }
        else
        {
            if (a[i]-k<=0)
            break;
            summ=summ+a[i]-k;
            step=1;
        }
        ++k;

        /*for (int i=0; i<n; ++i)
        {
            cout << a[i] << " ";
        }
        cout << endl;
        cout << summ << " ";*/
    }
    cout << summ;
    return 0;
}