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

typedef int ll;
const ll INF = 100005;

ll n, dist;
ll dis[INF], a[INF], boxcount, counter, ans, box, tim, lastans;
//set<ll> box;

int main()
{
    counter = 0;
    boxcount = 0; ans = 0;
    cin >> n;
    for(int i=1; i <= n; i++){
        cin >> dis[i];

        if(dis[i] != dis[i-1]) {
            if(box != 1) ans+=box;
            box=0;
        }
        box++;

        tim = n-i;
        dist = dis[i] - boxcount;
        if(dist >= tim) {
            // << "first";
            if (box != 1){
                ans+=lastans;
                //cout << "problem";
            }
            else{
                lastans = dis[i] - tim + box - 1;
                ans+=lastans;
            }
        }
        else{
            //cout << "second";
            if (box != 1){
                ans+=lastans;
                //cout << "problem";
            }
            else{
                lastans = dis[i] - dist + box - 1;
                ans+=lastans;
            }
        }
        counter++;
        //cout << " " << ans << " " << lastans << endl;
        if(box == 1) boxcount++;
    }
    if(box != 1) ans+=box;
    cout << ans << endl;

    return 0;
}
