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 <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#define rep(i,n) for(int i=0;i<(n);++i)
#define rrep(i,n) for(int i=1;i<(n);++i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define dunk(a) cout << (a) << "\n"
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf = 1001001001;
const int mod = 1000000007;

int n;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	vector<ll> a(n);
	rep(i, n) cin >> a[i];
	sort(all(a));
	if (n == 2) {
		dunk(abs(a[0] - a[1]));
		return 0;
	}
	if (n == 3) {
		ll res1 = abs(a[0] - a[1]), res2 = abs(a[0] - a[2]), res3 = abs(a[1] - a[2]);
		ll ans = max({ res1 + res2,res2 + res3,res1 + res3 });
		dunk(ans);
		return 0;
	}
	if (n % 2 == 0) {
		deque<ll> que1, que2;
		ll k1 = 0, k2 = 0;
		rep(i, n) {
			if (i + 1 == n / 2) {
				que1.emplace_back(a[i]);
				k1 = a[i];
				continue;
			}
			if (i == n / 2) {
				que2.emplace_back(a[i]);
				k2 = a[i];
				continue;
			}
			que1.emplace_back(a[i]);
			que2.emplace_back(a[i]);
		}
		ll res1 = 0, res2 = 0;
		rep(i, n - 1) {
			if (i % 2 == 0) {
				ll tmp = que1.back(); que1.pop_back();
				res1 += abs(k2 - tmp);
				k2 = tmp;
			}
			else {
				ll tmp = que1.front(); que1.pop_front();
				res1 += abs(k2 - tmp);
				k2 = tmp;
			}
		}
		rep(i, n - 1) {
			if (i % 2 == 0) {
				ll tmp = que2.back(); que2.pop_back();
				res2 += abs(k1 - tmp);
				k1 = tmp;
			}
			else {
				ll tmp = que2.front(); que2.pop_front();
				res2 += abs(k1 - tmp);
				k1 = tmp;
			}
		}
		ll ans = max(res1, res2);
		dunk(ans);
	}
	else {
		deque<ll> res;
		ll k = 0;
		rep(i, n) {
			if (i == (n - 1) / 2) {
				k = a[i];
				continue;
			}
			res.emplace_back(a[i]);
		}
		ll ans = 0;
		rep(i, n - 1) {
			if (i % 2 == 0) {
				ll tmp = res.back(); res.pop_back();
				ans += abs(k - tmp);
				k = tmp;
			}
			else {
				ll tmp = res.front(); res.pop_front();
				ans += abs(k - tmp);
				k = tmp;
			}
		}
		dunk(ans);
	}
	return 0;
}