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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Linq;
using static System.Math;
using System.Collections.Generic;
using Li = System.Collections.Generic.List<int>;
using Ll = System.Collections.Generic.List<long>;

namespace AtCoder0705
{
	class Program
	{
		static void Main(string[] args)
		{
			D();
		}

		private static void D()
		{
			var n = int.Parse(Console.ReadLine());
			var a = ReadLongs();
			Array.Sort(a);
			var bigger = a.Skip(a.Length / 2).ToArray();
			var sumB = bigger.Sum();
			long result = 2 * sumB - bigger[bigger.Length - 1] - (a.Length % 2 == 0 ? 0 : bigger[0]);

			Console.WriteLine(result);
		}

		static int[] bestPerm;
		static Li besC;

		private static long SolveDBrutal(int[] a)
		{
			var permutations = GetPermutations(a);
			long score = 0;

			foreach (var p in permutations)
			{
				var current = new Li { p[0] };
				SolveDBrutalRec(p, current, 0, ref score);
			}

			return score;
		}

		private static void SolveDBrutalRec(int[] p, Li current, long score, ref long bestScore)
		{
			if (current.Count == p.Length)
			{
				if (score > bestScore)
				{
					besC = current.ToList();
					bestPerm = p;
					bestScore = score;
				}
				return;
			}

			for (int i = 0; i < current.Count; i++)
			{
				current.Insert(i, p[current.Count]);
				var left = current[(i - 1 + current.Count) % current.Count];
				var right = current[(i + 1) % current.Count];
				var scoreL = score + Min(left, right);
				SolveDBrutalRec(p, current, scoreL, ref bestScore);
				current.RemoveAt(i);
			}
		}

		public static List<int[]> GetPermutations(int[] list)
		{
			int x = list.Length - 1;
			var result = new List<int[]>();
			GetPermutations(list, result, 0, x);
			return result;
		}

		private static void GetPermutations(int[] list, List<int[]> result, int k, int m)
		{
			if (k == m)
				result.Add(list.ToArray());
			else
				for (int i = k; i <= m; i++)
				{
					Swap(ref list[k], ref list[i]);
					GetPermutations(list, result, k + 1, m);
					Swap(ref list[k], ref list[i]);
				}
		}

		public static void Swap<T>(ref T a, ref T b)
		{
			T t = a;
			a = b;
			b = t;
		}

		private static void A()
		{
			var n = int.Parse(Console.ReadLine());
			Console.WriteLine((1000 - n % 1000) % 1000);
		}

		private static void B()
		{
			var n = int.Parse(Console.ReadLine());
			var strs = Enumerable.Range(0, n).Select(x => Console.ReadLine()).ToArray().GroupBy(x => x).Select(g => Tuple.Create(g.Key, g.Count())).ToArray();
			var ac = strs.Where(x => x.Item1 == "AC").Select(x => x.Item2).FirstOrDefault();
			var wa = strs.Where(x => x.Item1 == "WA").Select(x => x.Item2).FirstOrDefault();
			var tle = strs.Where(x => x.Item1 == "TLE").Select(x => x.Item2).FirstOrDefault();
			var re = strs.Where(x => x.Item1 == "RE").Select(x => x.Item2).FirstOrDefault();

			Console.WriteLine($"AC x {ac}");
			Console.WriteLine($"WA x {wa}");
			Console.WriteLine($"TLE x {tle}");
			Console.WriteLine($"RE x {re}");
		}


		private static void C()
		{
			var ints = ReadInts();
			var h = ints[0];
			var w = ints[1];
			var k = ints[2];

			var ar = Enumerable.Range(0, h).Select(x => Console.ReadLine().Select(c => c == '#').ToArray()).ToArray();
			var rowCounts = ar.Select(x => x.Count(y => y)).ToArray();
			var columnCounts = Enumerable.Range(0, w).Select(i => Enumerable.Range(0, h).Count(j => ar[j][i])).ToArray();

			var countResult = 0;
			for (int i = 0; i < 1 << (h + w); i++)
			{
				var arCopy = ar.Select(x => x.ToArray()).ToArray();
				for (int j = 0; j < h; j++)
					if ((i & (1 << j)) != 0)
						for (int l = 0; l < w; l++)
							arCopy[j][l] = false;

				for (int j = h; j < w + h; j++)
					if ((i & (1 << j)) != 0)
						for (int l = 0; l < h; l++)
							arCopy[l][j - h] = false;

				var c = arCopy.Sum(x => x.Count(y => y));
				if (c == k)
					countResult++;
			}

			Console.WriteLine(countResult);
		}

		private static int[] ReadInts() => Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
		private static long[] ReadLongs() => Console.ReadLine().Split().Select(x => long.Parse(x)).ToArray();

	}
}