#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef string str;
typedef pair<ll, ll> pll;
const ll MAXNUM=100007;

ll n, m, k, t, block[MAXNUM], last, num_last, first, sum, curr_num, shift;

int main()
{
    scanf("%lld", &t);
    for(int u=1; u<=t; u++){
        scanf("%lld", &n);
        block[u]=n;
    }
    first=t;
    shift=0;
    while(block[first]>num_last){
        sum+=block[first]-shift;
        first--;
        num_last++;
        if(block[first]!=block[first+1]) shift=num_last;
        if(block[first]==num_last) num_last--;
        while(block[last+1]<=num_last) last++;
    }
    cout << sum;
    return 0;
}
