#include <iostream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long ll;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("inA.txt", "r", stdin);

    ll n;
    cin >> n;
    vector<ll> a(n);
    for (auto &i : a)
        cin >> i;

    ll res = 0, add = 0, eq = 0;
    for (ll i = n-1; i >= 0; i--) {
        if (i == n-1 || a[i] != a[i+1])
            eq = 1;
        if (eq == 1 && a[i] - add <= 0) {
            ll w = a[i];
            while (i >= 0 && a[i] == w)
                i--;
            continue;
        }
        if (i < n-1 && a[i] == a[i+1])
            eq++;
        if (a[i] - add + eq-1 > 0) {
            res += a[i] - add + eq-1;
            add++;
        }
    }
    cout << res;

    return 0;
}