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

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using static Atcoder_Template_NetCore3.MyBitUtil;
using static Atcoder_Template_NetCore3.MyInputUtil;
using static System.Console;

namespace Atcoder_Template_NetCore3
{
    class Program
    {
        static void Main()
        {
            var count = ReadInput<int>();
            var hp = (int)Math.Ceiling(count / 2d);

            var input = ReadArray<int>();
            var sorted = input.OrderByDescending(n => n).ToArray();

            long ans = 0;
            for (int i = 0; i < hp; i++)
            {
                if (i == 0) continue;
                ans += sorted[i - 1];
            }

            var insMax = sorted[hp];
            var linkCount = hp - 1;

            for (int i = 0; i < linkCount; i++)
            {
                var min = sorted[i + 1];
                ans += min;
            }
            if (count % 2 == 0) ans += insMax;

            WriteLine(ans);
        }
    }

    /// <summary>
    /// Bit取得、およびbit全探索を提供します。
    /// </summary>
    internal static class MyBitUtil
    {
        /// <summary>
        /// 指定の桁のビットを取得します。
        /// </summary>
        /// <param name="count">取得する桁。</param>
        /// <returns></returns>
        public static int GetBit(this int num, int count)
        {
            var x = num >> (count - 1);
            const int mask = 1;
            return x & mask;
        }

        /// <summary>
        /// 各ビットの値を <see cref="int"/> 配列として取得します。
        /// 下位の桁が配列の前に来ます。
        /// </summary>
        public static int[] ToBitArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new int[size];
            for (var i = 0; i < size; i++)
            {
                array[i] = num.GetBit(i + 1);
            }
            return array;
        }

        /// <summary>
        /// 各ビットを真偽値として解釈した結果を <see cref="bool"/> 配列として取得します。
        /// 下位の桁が配列の前に来ます。
        /// </summary>
        public static bool[] ToBoolArray(this int num)
        {
            const int size = sizeof(int) * 8;
            var array = new bool[size];
            for (var i = 0; i < size; i++)
            {
                array[i] = num.GetBit(i + 1) == 1;
            }
            return array;
        }

        /// <summary>
        /// Bit全探索を行います。
        /// </summary>
        /// <param name="count">対象とする要素の個数。30以下である必要があります。</param>
        /// <param name="action">各パターンに対して行う処理</param>
        public static void BitAllSearch(int count, Action<bool[]> action)
        {
            var max = 1 << count;
            for (var i = 0; i < max; i++)
            {
                action.Invoke(i.ToBoolArray());
            }
        }

        /// <summary>
        /// 指定したコレクションに対してBit全探索を行います。
        /// </summary>
        /// <param name="list">対象とする集合。要素数30以下である必要があります。</param>
        /// <param name="action">各パターンに対して行う処理</param>
        public static IEnumerable<IEnumerable<T>> GetAllPatterns<T>(IReadOnlyList<T> list)
        {
            var max = 1 << list.Count;
            for (var i = 0; i < max; i++)
            {
                var usePattern = i.ToBoolArray();
                IEnumerable<T> query = list.Where((item, j) => usePattern[j]);
                yield return query;
            }
        }
    }

    /// <summary>
    /// 標準入力の高機能な読み取りを提供します。
    /// </summary>
    internal static class MyInputUtil
    {
        private const char separator = ' ';

        /// <summary>
        /// 標準入力から入力を1つ指定の型に変換して読み取ります。
        /// </summary>
        /// <typeparam name="T">変換先として指定する型。</typeparam>
        /// <param name="converter">変換に使用する関数。<c>null</c>の場合は自動決定を試みます。</param>
        /// <returns></returns>
        public static T ReadInput<T>(Func<string, T> converter = null)
        {
            if (converter is null)
            {
                converter = GetConverter<T>();
            }
            return converter.Invoke(Console.ReadLine());
        }

        #region 一定個数の入力の読み取り
        // 用法は ReadInput<T> と共通です。
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T> ReadInput2<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T> ReadInput3<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T> ReadInput4<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T> ReadInput5<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T, T> ReadInput6<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]), converter.Invoke(input[5]));
        }
        [GeneratedCode(null, "2.1")]
        public static ValueTuple<T, T, T, T, T, T, T> ReadInput7<T>(Func<string, T> converter = null)
        {
            if (converter is null) { converter = GetConverter<T>(); }
            var input = Console.ReadLine().Split(separator);
            return (converter.Invoke(input[0]), converter.Invoke(input[1]), converter.Invoke(input[2]), converter.Invoke(input[3]), converter.Invoke(input[4]), converter.Invoke(input[5]), converter.Invoke(input[6]));
        }
        #endregion

        /// <summary>
        /// 標準入力から配列を読み取ります。
        /// </summary>
        /// <returns></returns>
        public static T[] ReadArray<T>(Func<string, T> converter = null)
        {
            var str = ReadLine().Split(separator);
            if (converter is null)
            {
                converter = GetConverter<T>();
            }
            return str.Select(s => converter.Invoke(s)).ToArray();
        }

        #region 変換用メソッド
        /// <summary>
        /// 文字列を指定の型に変換します。
        /// </summary>
        /// <typeparam name="T">変換先の型。すべての組み込み型がサポートされます。</typeparam>
        /// <param name="str">変換元の文字列</param>
        /// <exception cref="NotSupportedException">指定の型引数がサポートされない場合にスローされる例外。</exception>
        /// <remarks>大量の値を処理する場合、このメソッドは低速です。<see cref="ConvertTo{T}(string[])"/> の使用を検討してください。</remarks>
        public static T ConvertTo<T>(string str)
        {
            return GetConverter<T>().Invoke(str);
        }

        /// <summary>
        /// 文字列配列を指定の型の配列に変換します。
        /// </summary>
        /// <typeparam name="T">変換先の型。すべての組み込み型がサポートされます。</typeparam>
        /// <param name="str">変換元の文字列配列</param>
        /// <exception cref="NotSupportedException">指定の型引数がサポートされない場合にスローされる例外。</exception>
        public static T[] ConvertTo<T>(string[] str)
        {
            var conveter = GetConverter<T>();
            var array = new T[str.Length];
            for (var i = 0; i < str.Length; i++)
            {
                array[i] = conveter.Invoke(str[i]);
            }
            return array;
        }

        private static Func<string, T> GetConverter<T>() => GetConverterOrDefault<T>() ?? throw new NotSupportedException("この型に対する変換関数の取得はサポートされていません");
        private static bool TryGetConverter<T>(out Func<string, T> func)
        {
            func = GetConverterOrDefault<T>();
            return !(func is null);
        }

        private static Func<string, T> GetConverterOrDefault<T>()
        {
            Type type = typeof(T);
            Func<string, T> conveter;

            // 出現頻度が高そうなものを上
            if (type == typeof(int))
            {
                conveter = str => (T)(object)int.Parse(str);
            }
            else if (type == typeof(long))
            {
                conveter = str => (T)(object)long.Parse(str);
            }
            else if (type == typeof(double))
            {
                conveter = str => (T)(object)double.Parse(str);
            }
            else if (type == typeof(decimal))
            {
                conveter = str => (T)(object)decimal.Parse(str);
            }
            else if (type == typeof(float))
            {
                conveter = str => (T)(object)float.Parse(str);
            }
            else if (type == typeof(char))
            {
                conveter = str => (T)(object)str[0];
            }
            else if (type == typeof(short))
            {
                conveter = str => (T)(object)short.Parse(str);
            }
            else if (type == typeof(byte))
            {
                conveter = str => (T)(object)byte.Parse(str);
            }
            else if (type == typeof(bool))
            {
                conveter = str => (T)(object)bool.Parse(str);
            }
            else if (type == typeof(ulong))
            {
                conveter = str => (T)(object)ulong.Parse(str);
            }
            else if (type == typeof(uint))
            {
                conveter = str => (T)(object)uint.Parse(str);
            }
            else if (type == typeof(ushort))
            {
                conveter = str => (T)(object)ushort.Parse(str);
            }
            else if (type == typeof(sbyte))
            {
                conveter = str => (T)(object)sbyte.Parse(str);
            }
            else if (type == typeof(string))
            {
                conveter = str => (T)(object)str;
            }
            else if (type == typeof(object))
            {
                conveter = str => (T)(object)str;
            }
            else
            {
                conveter = null;
            }

            return conveter;
        }
        #endregion
    }
}