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.Linq;
using System.Collections;
using System.Collections.Generic;

namespace ABC173_D
{
    using static Ex;
    public class D
    {
        /// <summary>
        /// N人の心地よさの合計の最大値
        /// </summary>
        static long ans;

        /// <summary>
        /// プレイヤーN人
        /// フレンドリーさ は A i
        ///
        /// 割り込んだ位置から到着した時点で「時計回りで最も近い人」と「反時計回りで最も近い人」のフレンドリーさのうち
        /// 小さい方に等しい 心地よさ
        /// 最初に到着した人の心地よさは 0
        /// </summary>
        public static void Main(string[] args)
        {
            int n = ReadInt;
            var a = ReadLongArray().OrderByDescending(v => v).ToArray();

            ans -= a.Last();
            ans += a.Sum();

            ans.Cw();
        }
    }

    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }

        public static long Lcm(int x, int y) { return x / Gcd(x, y) * y; }
        public static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var v = new string[N]; for (long i = 0; i < N; ++i) v[i] = Read; return v; }
        public static int[] ReadIntArray(long N) { var v = new int[N]; for (long i = 0; i < N; ++i) v[i] = ReadInt; return v; }
        public static long[] ReadLongArray(long N) { var v = new long[N]; for (long i = 0; i < N; ++i) v[i] = ReadLong; return v; }

        public static (T, T) ToTpl2<T>(this T[] v) { return (v[0], v[1]); }
        public static (T, T, T) ToTpl3<T>(this T[] v) { return (v[0], v[1], v[2]); }
        public static (T, T, T, T) ToTpl4<T>(this T[] v) { return (v[0], v[1], v[2], v[3]); }
        public static (T, T, T, T, T) ToTpl5<T>(this T[] v) { return (v[0], v[1], v[2], v[3], v[4]); }
    }
}