#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	ll n, a[100000] = {};
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    ll t = 1;
    vector <ll> v, w;
    for (int i = 1; i < n; i++)
    {
        if (a[i] == a[i - 1])
            t++;
        else
        {
            v.push_back(t);
            t = 1;
        }
    }
    v.push_back(t);
    w.push_back(v[0]);
    for (int i = 1; i < v.size(); i++)
        w.push_back(w[i - 1] + v[i]);

    ll r = 0;
    for (int i = w.size() - 1; i >= 0; i--)
    {
        if (a[w[i] - 1] - n + w[i] < 1)
            break;
        else
            r += (a[w[i] - 1] - n + w[i]) * v[i];
        //cout << a[w[i] - 1] << ' ' << v[i] << endl;
    }

    cout << r << "\n";
}
