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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define A_DIGITS_MAX    9

typedef struct friend_st {  //時計回り方向の循環ノード
    int friendly;
    struct friend_st *prev; //直前のノードポインタ
    struct friend_st *next; //直後のノードポインタ
} friend_t;

void merge_sort(int *a, int N, int left, int right){
    int mid, i, j, k;
    int temp[N];

    if(left >= right)
    return;

    mid = (left + right) / 2;
    merge_sort(a, N, left, mid);
    merge_sort(a, N, mid+1, right);

    i = left;
    j = mid+1;
    k = left;
    while(i != mid+1 && j != right+1){
        if(a[i] < a[j]){        //降順にソートする
            temp[k] = a[j];
            j++;
            k++;
        }
        else if(a[i] == a[j]){
            temp[k] = a[i];
            i++;
            k++;
            temp[k] = a[j];
            j++;
            k++;
        }
        else{
            temp[k] = a[i];
            i++;
            k++;
        }
    }

    if(i == mid+1){
        while(j != right+1){
            temp[k] = a[j];
            j++;
            k++;
        }
    }
    else if(j == right+1){
        while(i != mid+1){
            temp[k] = a[i];
            i++;
            k++;
        }
    }

    while(k>left){
        k--;
        a[k] = temp[k];
    }
}

int add_node(friend_t *head, int A){
    friend_t *new, *start = head->next, *prev = head, *next = start, *node;
    int max;

    if(head->friendly < start->friendly) max = head->friendly;
    else max = start->friendly;

    if((new = (friend_t *)malloc(sizeof(friend_t))) == NULL) return -1;

    for(node = start; node != head; node = node->next){
        if(node->friendly < (node->next)->friendly){
            if(max < node->friendly) {
                max = node->friendly;
                prev = node;
                next = node->next;
            }
        }
        else{
            if(max < (node->next)->friendly){
                max = (node->next)->friendly;
                prev = node;
                next = node->next;
            }
        }
    }

    new->friendly = A;
    new->prev = prev;
    new->next = next;

    prev->next = new;
    next->prev = new;

    return max;
}

int main(void) {
    int N, i;
    int *A;
    char ch_N[6];

    /*read input-------------------------------------*/
    fgets(ch_N, 6, stdin);
    N = atoi(ch_N);

    if((A = (int *)malloc(sizeof(int) * N)) == NULL){
        printf("malloc A can't work\n");
        return -1;
    }

    char *input;
    unsigned long int M = (N+1) * A_DIGITS_MAX;

    if((input = (char *)malloc(sizeof(char) * M)) == NULL){
        printf("malloc input can't work\n");
        return -1;
    }

    fgets(input, M, stdin);

    int n = 0, chof = 0, chof2 = 0;
    char num[A_DIGITS_MAX + 1];

    while(n < N){
        if(input[chof] == ' ' || input[chof] == '\0'){
            num[chof2] = '\0';
            A[n] = atoi(num);
            n++;
            chof2 = 0;
        }
        else{
            num[chof2] = input[chof];
            chof2++;
        }
        chof++;
    }
    free(input);
    /*read input-------------------------------------*/

    merge_sort(A, N, 0, N-1);

    friend_t *head;

    if((head = (friend_t *)malloc(sizeof(friend_t))) == NULL){
        printf("malloc head can't work\n");
        return -1;        
    }

    head->friendly = A[0];
    head->prev = head;
    head->next = head;

    int sum = 0, temp;

    for(i = 1; i < N; i++){
        if((temp = add_node(head, A[i])) == -1){
            printf("malloc new node can't work\n");
            return -1;         
        }
        sum += temp;        
    }

    printf("%d\n", sum);

    /*free----------------------------------*/
    friend_t *node = head->next, *node2;

    while(node != head){
        node2 = node->next;
        free(node);
        node = node2;
    }
    free(head);

    free(A);

    return 0;
}