#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;
//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]) box=0;
        box++; a[i] = dis[i];

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

    return 0;
}
