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 <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#include <cmath>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define LL long long
using namespace std;
ll a[1000000];
int main(){
    /*ll n;
    sld(n);
    if(n >= 4){
        puts("No");
    }else if(n == 3){
        puts("Yes");
        puts("3");
        puts("2 1 2");
        puts("2 3 1");
        puts("2 2 3");
    }else if(n == 2){
        puts("No");
    }else if(n == 1){
        puts("Yes");
        puts("2");
        puts("1 1");
        puts("1 1");
    }*/
    ll n;
    sld(n);
    for(ll i = 0;  i <n; i ++){
        sld(a[i]);
    }
    sort(a,a+n);
    ll sum = 0;

    //pld(sum) pn
    ll r = 0,l = 0;
    if(n == 2){
        pld(a[1]-a[0]) pn return 0;
    }else if(n == 3){
        pld(a[2]+a[1]-2*a[0]) pn return 0;
    }else if(n == 4){
        pld(a[3]*2+a[2]-2*a[0]-a[1]) pn return 0;
    }else if(n & 1){
        //sum += a[n-1]-a[0]+a[n-2]-a[0];
        l = 0,r = n-1;
        while(1){
                sum += a[r]-a[l];
            l += 2;
            if(r == l){
                goto l0;
            }
            sum += a[r]-a[l];
            r -= 2;
            if(r == l){
                goto l0;
            }

        }
        l0:0;

        l = 1,r = n-2;
        while(1){
            sum += a[r]-a[l];
            r -= 2;
            if(r == l){
                goto l1;
            }
            sum += a[r]-a[l];
            l += 2;
            if(r == l){
                goto l1;
            }
        }
        l1:0;
        //pld(sum) pn
        pld(sum+a[n-2]-a[0]) pn
    }else{
        //sum += a[n-1]-a[0]+a[n-2]-a[0];
        l = 1,r = n-1;
        while(1){
            sum += a[r]-a[l];
            r -= 2;
            if(r == l){
                goto l2;
            }
            sum += a[r]-a[l];
            l += 2;
            if(r == l){
                goto l2;
            }
        }
        l2:0;
        l = 0,r = n-2;
        while(1){

            sum += a[r]-a[l];
            l += 2;
            if(r == l){
                goto l3;
            }
            sum += a[r]-a[l];
            r -= 2;
            if(r == l){
                goto l3;
            }
        }
        l3:0;
        pld(sum+a[n-1]-a[0]) pn
    }





    return 0;
}