#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, st, fn, en;
//set<ll> box;

int main()
{
    ans = 0;
    cin >> n;
    tim = 0; box = 0; en=1;
    for(int i=1; i <= n; i++){
        cin >> dis[i];
    }
    for(int i=n; i >= 1; i--){
        //cout << "dis[i] = " << dis[i] << " dis[i-1] = " << dis[i-1] << endl;

        if(dis[i] > tim){
            ans+=dis[i]-tim;
        }
        if(dis[i] == dis[i-1]) { tim--; box++; en=0; }
        //else if(dis[i] != dis[i-1] && en == 0) { tim--; box++; en=1; }
        else { tim+=box; box=0; en=1; }
        tim++;
        //cout << tim << " " << box << " -->> " << ans << endl;
    }

    cout << ans << endl;

    return 0;
}
