#include <bits/stdc++.h>
#define ll long long 
#define ull unsigned ll
#define dbl double

using namespace std;

const int N = 1e5 + 10;
deque < pair <ll, ll> > v;

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

	ll n;
    cin >> n;
    ll prevx = 0;
    ll cnt = 1;
    for (ll i = 1; i <= n; ++i)
    {
        ll x;
        cin >> x;
        if (prevx == x)
            ++cnt;
        else
        {
            if (prevx != 0)
                v.push_back({prevx, cnt});
            prevx = x;
            cnt = 1;
        }
    }
    v.push_back({prevx, cnt});

    ll cur = v.size() - 1;
    ll cost = 0;
    while (v.size() != 0)
    {
        //for (ll i = 0; i < v.size(); ++i)
        //    cout << v[i].first << " " << v[i].second << endl; 
        //cout << endl;

        cur = v.size() - 1;
        cost += v[cur].first * v[cur].second;
        ll cnt = v[cur].second;
        //cout << "cost now is " << cost << endl;
        v.pop_back();
        for (ll i = v.size() - 1; i >= 0; --i)
        {
            ll null = 0;
            v[i].first = max(null, v[i].first - cnt);
            if (v[i].first == 0)
                for (ll j = 0; j <= i; ++j)
                    v.pop_front();
        }
    }
    cout << cost;
}
