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)

/**
 *    Author: "Robin Singh" !!
 *    BrainyIsTheNewSexy
 *    rob_in_1
**/

#include <bits/stdc++.h>
using namespace std;

#define dbg( args...) { vector<string> _v = split( #args, ','); err( _v.begin(), args); cerr << '\n'; }
vector<string> split( const string& s, char c) { vector<string> v; stringstream ss( s); string x; 
while ( getline( ss, x, c)) v.emplace_back(x); return move(v); }
void err( vector<string>::iterator it) { } 
template<typename T, typename... Args>
void err( vector<string>::iterator it, T a, Args... args) {
cerr << it -> substr( ( *it)[0] == ' ', it -> length()) << " = " << a << '\t'; err( ++it, args...); }

#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define ff first
#define ss second
#define MP make_pair
#define szz(v) ((int)(v).size())  
#define fr(i, j, k) for(int i = j; i < k; i++)     
#define bk(i, j, k) for(int i = j-1; i >= k; i--)
#define mem(ptr, val)  memset(ptr, val, sizeof ptr)
#define sbit(n)  __builtin_popcount(n)
#define nl "\n"

typedef long double ld;
typedef pair<int, int> pii;
typedef vector <int> vi;
typedef vector < pair<int,int> > vpii;
const long double eps = 1e-9;
const int mod = 1e9+7;

pii dir[8] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1} , {-1, 1}, {1, -1} };

//*****************************************!Template Ends Here!******************************************]

const int nn = 2e5+5;

void solve(){
	int n = 0, m = 0, cnt = 0, sum = 0, mx = -1e18, mn = 1e18, k = 0, ans = 0;
    cin >> n;
    int a[n+1];
    fr(i, 1, n+1) cin >> a[i];
    sort(a+1, a+n+1);
    if(n % 2 == 0){
        for(int i=n; i>=1; i--){
            if(i == n/2 + 1) ans += a[i];
            else if(i == n/2) ans -= a[i];
            else if(i > n/2) ans += a[i]*2;
            else ans -= a[i]*2;
        }
    }
    else{
        if(n == 3){
            ans += a[3]-a[1] + a[2]-a[1];
        }
        else{
        for(int i=n; i>=1; i--){
            if(i == n/2 + 1) ans -= a[i];
            else if(i == n/2) ans -= a[i];
            else if(i > n/2) ans += a[i]*2;
            else ans -= a[i]*2;
        }
        }

    }
    cout << ans;
}

int32_t main()
{
    // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t = 1;
    // cin >> t;
    for(int tc = 1; tc <= t; tc++){
    	// cout << "Case #" << tc << ": ";
    	solve();
    }
    return 0;
}