#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<ll> a(n);
    for (ll &i : a) cin >> i;
    ll res = 0, d = 0, dc = 0;
    int last = -1;
    for (int i = n - 1; i >= 0; --i)
    {
        if (a[i] != last)
        {
            d += dc;
            dc = 0;
        }
        res += max(a[i] - d, 0ll);
        ++dc;
        last = a[i];
    }
    cout << res;
    return 0;
}
