Problem:
Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6

Solution in C++ (GCC 9.2.1) (judge verdict: Accepted)

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

#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define fr(i,l,r) for (long long int i = l; i < r; ++i)
#define frr(i,l,r) for (long long int i = r; i >= l; --i) 
#define sortarr sort(arr,arr+n) //sorts array in asc order
#define sortarr_r sort(arr, arr+n, greater<long long int>()) //sorts array in desc order
#define make_set set<long long int>s //Set arranges in asc order
#define make_set_r set<long long int, greater <long long int>>s ////Set arranges in desc order
#define make_vector vector<long long int>v; 
#define max_heap priority_queue <int> pq; // max heap 
#define min_heap priority_queue <int, vector<int>, greater<int> > pq;  //min heap
#define make_pairVector vector<pair<long long int,long long int> > pairVector // vector of pairs
#define hashmap unordered_map<long long int, long long int>hashmap;
#define pb push_back

//transform(s.begin(), s.end(), s.begin(), ::tolower); //to convert a string entirely to lower case

//toupper(s[i]) or tolower(s[i]) // converting a character to upper or lower case

//map<pair<ll,ll>, ll > hash;          // pair,value hashmap where key is a pair
//hash[make_pair(i,j)] = 0;
//if(hash.find(make_pair(i,j)) == hash.end())


//map<ll,ll>::iterator itr;           // iterating a map
//for (itr = hash.begin();itr != hash.end(); itr++)
//{
//   itr->first = key  
//   itr->second = value
//}


// vector<string> v; //to convert vector to a set
// set<string> s(v.begin(), v.end());


// set<string>s;     // to convert set to a vector
// vector<string> v(s.begin(),s.end());


// cin >> x >> y;
// pairVector.pb(mp(x,y));


// string size = str.size()


// stack <int> s; 


// queue <int> gquiz; 


// __gcd(m, n) 

// ll arr[10e5 + 5];


// char arr[10e5 + 5];


//vec.insert(v.begin(),value)   -> to insert a value in the vector at the begin

// printf ("%.9f\n",x);
// cout << fixed << setprecision(9);

//covert integer to string :  to_string(integer)

bool sortbysec(const pair<long long int,long long int> &a, const pair<long long int,long long int> &b) 
{ 
    return (a.second < b.second); 
}
bool compare(string &s1,string &s2) 
{ 
    return s1.size() < s2.size(); 
} 
int check_key(map<long long int, long long int> m, long long int key) 
{ 
    // Key is not present 
    if (m.find(key) == m.end()) 
        return 0; 
  
    return 1; 
} 

void yes()
{
    cout << "YES" << endl;
}
void no()
{
    cout << "NO" << endl;
}

int main(void)
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    ll n;
    cin >> n;
    ll arr[n+5] = {0};
    for(ll i = 0; i<n; i++)
    {
        cin >> arr[i];
    }
    // sortarr_r;
    sort(arr, arr+n+0, greater<long long int>());
    // for(ll i = 0; i<n; i++)
    // {
    //     cout << arr[i] << endl;
    // }
    ll sum = 0;
    ll num = n;
    num-=2;
    for(ll i = 1; i<n; i++)
    {
        if(num > 0)
        {
            num--;
            sum+=arr[i];
        }
        if(num > 0)
        {
            num--;
            sum+=arr[i];
        }
    }
    cout << sum + arr[0] << endl;
    
    return 0;
}