#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;
//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];
        dist = dis[i] - boxcount;
        if(dist >= n-i) {
            //cout << "first" << endl;
            ans += dis[i] - n + i + box - 1;
        }
        else{
            //cout << "second" << endl;
            ans += dis[i] - dist + box - 1;
        }
        counter++;
        //cout << " " << ans << endl;
        if(box == 0) boxcount++;
    }
    cout << ans << endl;

    return 0;
}
