#include <bits/stdc++.h>

using namespace std;

const long long MAX = 1e18, MIN = -1e18;
int main ()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    long long n, x = 1, ans = 0, p = 0;
    cin >> n;
    long long a[n];
    for (long long i = 0; i < n; i++)
        cin >> a[i];
    reverse(a, a + n);
    for (long long i = 1; i < n; i++)
    {
        if (a[i] == 0) {x = 0; break;}
            else if (a[i] == a[i - 1]) x++;
                else
                {
                    ans += a[i - 1] * x;
                    for (long long j = i; j < n; j++)
                        if (a[j] - x > 0) a[j] -= x;
                            else a[j] = 0;
                    if (i + 1 == n) x = 0;
                        else x = 1;
                }
    }
    ans += a[n - 1] * x;
    cout << ans << endl;
    return 0;
}
