Problem:
Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<cstdio>
#include<math.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string.h>
 
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1001001001
#define LLINF 1001001001001001001
#define mp make_pair
#define LLIandI pair<long long int , int>
#define ll long long

int main(){
    long long int N;
    scanf("%lld",&N);
    long long int S[100002];
    rep(i,N){
        scanf("%lld",&S[i]);
    }
    sort(S,S+N);
    long long int ans=0;
    long long int al=1,ar=0;
    long long int bl=N-1,br=N-2;
    ans+=(S[N-1]-S[0])+(S[N-2]-S[0]);
    while(1){
        if(ar+1>=br)break;
        ans+=(S[bl]-S[ar+1]);al=ar+1;//hidari
        if(al+1>=br)break;
        ans+=(S[br]-S[al+1]);ar=al+1;//printf("%d\n",ans);//migi
        if(ar>=br-1)break;
        ans+=(S[br-1]-S[al]);bl=br-1;//printf("%d\n",ans);//hidaridai
        if(ar>=bl-1)break;
        ans+=(S[bl-1]-S[ar]);br=bl-1;//printf("%d\n",ans);//migidai
        if(max(ar,al)>=min(br,bl))break;
    }
    printf("%lld\n",ans);
}