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: Wrong Answer)


#include <bits/stdc++.h>
using namespace std;
#define ll                 long long
#define ull                unsigned long long
#define ld                 long double
#define fastio             ios_base::sync_with_stdio(false);cin.tie(NULL);
#define NO                 cout << "NO" << endl
#define YES                cout << "YES" << endl
#define vec                vector<ll>
#define pi                 pair<ll, ll>
#define intarr(arr, n)     ll arr[n]; for (ll i = 0; i < n; i++) cin >> arr[i]
#define intvec(v, n)       for (ll i = 0; i < n; i++) { ll x;cin >> x; v.pb(x);}
#define loop(i, a, b, c)   for (ll i = a; i < b; i += c)
#define pb                 push_back
#define mkp                make_pair
#define ins                insert
#define print(a, n, m)     for (ll i = n; i < m; i++) {  cout << a[i] << " "; }  cout << endl
#define endl               "\n"
#define mod2               998244353
#define mod                1000000007
#define mem(dp, a)         memset(dp, a, sizeof(dp));
#define all(v)             v.begin(), v.end()
#define ump                unordered_map<ll, ll>
#define ff                 first
#define ss                 second
#define PI                 3.14159265358979
#define minh               priority_queue<ll>
#define maxh               priority_queue<ll,vector<ll>,greater<ll>>
#define flush              cout<<flush

//If path exist b/w A and B - BFS, DFS

// Shortest Path - BFS(unweighted), Dijkstra, Bellman-ford
//                 Floyd-Warshall, A*

//To detect negative cycles - Bellman-ford, Floyd-Warshall
//DFS - Connectivity, count connected components,
//      Find Bridges/Articulation points


// struct mi {
//   ll v; explicit operator ll() const { return v; }
//   mi() { v = 0; }
//   mi(ll _v) {
//     v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
//     if (v < 0) v += MOD;
//   }
//   friend bool operator==(const mi& a, const mi& b) {
//     return a.v == b.v; }
//   friend bool operator!=(const mi& a, const mi& b) {
//     return !(a == b); }
//   friend bool operator<(const mi& a, const mi& b) {
//     return a.v < b.v; }

//   mi& operator+=(const mi& m) {
//     if ((v += m.v) >= MOD) v -= MOD;
//     return *this; }
//   mi& operator-=(const mi& m) {
//     if ((v -= m.v) < 0) v += MOD;
//     return *this; }
//   mi& operator*=(const mi& m) {
//     v = v*m.v%MOD; return *this; }
//   mi& operator/=(const mi& m) { return (*this) *= inv(m); }
//   friend mi pow(mi a, ll p) {
//     mi ans = 1; assert(p >= 0);
//     for (; p; p /= 2, a *= a) if (p&1) ans *= a;
//     return ans;
//   }
//   friend mi inv(const mi& a) { assert(a.v != 0);
//     return pow(a,MOD-2); }

//   mi operator-() const { return mi(-v); }
//   mi& operator++() { return *this += 1; }
//   mi& operator--() { return *this -= 1; }
//     mi operator++(int) { mi temp; temp.v = v++; return temp; }
//     mi operator--(int) { mi temp; temp.v = v--; return temp; }
//   friend mi operator+(mi a, const mi& b) { return a += b; }
//   friend mi operator-(mi a, const mi& b) { return a -= b; }
//   friend mi operator*(mi a, const mi& b) { return a *= b; }
//   friend mi operator/(mi a, const mi& b) { return a /= b; }
//     friend ostream& operator<<(ostream& os, const mi& m) {
//         os << m.v; return os;
//     }
//     friend istream& operator>>(istream& is, mi& m) {
//         ll x; is >> x;
//         m.v = x;
//         return is;
//     }
// };

ll Ceil(ll a, ll b)
{
  if (a % b == 0)
    return a / b;
  else
    return a / b + 1;
}

ll gcd(ll a, ll b)
{
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll moduloInverse(ll a)
{
  ll p = mod;
  ll b = p - 2, ans = 1;
  for (; b > 0; a = a * a % p, b >>= 1)
    if (b & 1)
      ans = ans * a % p;
  return ans;
}

ll ncrmod(ll n, ll m)
{
  ll p = mod;
  if (m > n)
    return 0;
  ll up = 1, down = 1;
  for (ll i = n - m + 1; i < n + 1; i++)
    up *= i, up %= p;
  for (ll i = 1; i < m + 1; i++)
    down *= i, down %= p;
  return up * moduloInverse(down) % p;
}

ll powmod(ll a, ll n)
{
  if (!n)
    return 1;
  ll pt = powmod(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}

void factorize(long long int n)
{
  ll sum2 = 0, sum = 0;
  ll count = 0;

  while (!(n % 2)) {
    n >>= 1;
    count++;
  }

  if (count)
  {
    sum2 += count;
  }

  for (long long i = 3; i <= sqrt(n); i += 2) {
    count = 0;
    while (n % i == 0) {
      count++;
      n = n / i;
    }
    if (count)
    {
      if (i & 1)sum += count;
    }
  }
  if (n > 2)
  {
    sum++;
  }
}

bool isPrime(ll n)
{
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}

const ll  N = 5e5 + 5;
ll fact[N];
ll invfact[N];

void factorialInverse()
{
  fact[0] = 1;
  for (ll i = 1; i < N; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
  invfact[N - 1] = moduloInverse(fact[N - 1]);
  for (ll i = N - 2; i >= 0; i--) {
    invfact[i] = invfact[i + 1] * (i + 1);
    invfact[i] %= mod;
  }
}

ll ncr(ll n, ll r) {
  if (n < 0 or r < 0)
    return 0LL;
  if (n < r)
    return 0LL;
  return ((fact[n] % mod) * (invfact[r] % mod * invfact[n - r] % mod) % mod) % mod;
}


map<ll, vec > g;
map<ll, bool> visited;
ll ans[N + 1];

void dfs(ll i) {
  visited[i] = true;
  //cout << i << "-> ";
  for (auto j : g[i]) {
    if (visited[j] == false) {
      dfs(j);
      ans[i] = ans[i] + ans[j];
    }
  }
}

void bfs(ll s) {
  queue<ll> q;

  visited[s] = true;
  q.push(s);

  while (!q.empty()) {
    ll front = q.front();
    cout << front << " ";
    q.pop();
    for (auto i : g[front]) {
      if (!visited[i]) {
        visited[i] = true;
        q.push(i);
      }
    }
  }
}

void solve()

{
  ll n;
  cin >> n;
  intarr(a, n);
  sort(a, a + n, greater<int>());
  ll ans = 0;

  if (n == 2) {
    cout << a[0] << endl;
    return;
  }
  if (n == 3) {
    cout << a[0] + a[1] << endl;
    return;
  }
  //print(a, 0, n);
  ans = a[0] + a[1];
  n -= 3;
  ll count = 0;
  ll j = 2;
  for (ll i = 0; i < n; i++) {
    ans += a[j];
    count++;
    if (count == 2) {
      count = 0;
      j++;
    }
  }
  cout << ans;
}

int main()

{
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fastio;
  ll t;
  t = 1;
  //cin >> t;
  while (t--)
  {
    solve();
  }

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

  return 0;
}