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

int n, buf, balls[100000], cost = 0;

void grab(){
    int buf = balls[0], k = 0;
    for (int i = 0; i < n; i++) {
        if (buf < balls[i]) {
            buf = balls[i];
            k = 1;
        } else k++;
    }
    cost += buf*k;
    n-=k;
    int m = n;
    for (int i = 0; i < n; i++) {
        if (balls[i]-k>=0) balls[i]-=k;
        else balls[i] = 0;
    }
    n = m;
}

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

	
	cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> balls[i];
    }
    grab();
    grab();
    cout << cost;
}
