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: Accepted)

#include <bits/stdc++.h>
#include <stdint.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/rope>
#define int int_fast64_t

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef pair<int, int> pii;
typedef vector<int> VI;

#define FOR(i, j, k, l) for(int (i) = (j) - ((l) < 0); (l) < 0 ? (i) >= (k) : (i) < (k); (i) += (l))
#define RREP(i, j, k) for(int (i) = (j) - 1; (i) >= (k); --(i))
#define REP(i, j, k) for(int (i) = (j); (i) < (k); ++(i))
//#define VIS(it, con) for(__typeof((con).begin()) (it) = (con).begin(); (it) != (con).end(); ++(it))
#define VIS(x, y) for(__typeof(y.begin()) x = y.begin(); x != y.end(); ++x)
#define ALL(x) (x).begin(), (x).end()
#define MST(a, b) memset((a), (b), sizeof(a))
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fir first
#define sec second
#define endl '\n'

// #define _debug
#ifdef _debug
	#define IOS() (void)0
	#define de(...) cout << __VA_ARGS__
    #define ar(a, s, t) { REP(_i, (s), (t)) de((a)[_i] << ' '); de(endl); }
#else
	#define IOS() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); }
	#define de(...) (void)0
    #define ar(...) (void)0
#endif

const int maxn = 1e5 + 9;
int n, res = 0, a[maxn];

main() {
	IOS();
	cin >> n;
	REP(i, 0, n) cin >> a[i];
	sort(a, a + n);
	REP(i, 1, n) res += min(i, n - i) * 2 * (a[i] - a[i - 1]);
	if(n % 2 == 0) res -= a[n / 2] - a[n / 2 - 1];
	else res -= min(a[n / 2] - a[n / 2 - 1], a[n / 2 + 1] - a[n / 2]);
	cout << res << endl;
}