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)

#define _CRT_SECURE_NO_WARNINGS
#define mp	make_pair
#define pb	push_back
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

typedef long long	ll;

using namespace std;

vector<ll> a, b, vec, a1, b1;
ll n;

ll minim() {
	ll l = 1, r = n - 3;
	bool e = true;
	a.pb(vec[0]);
	a.pb(vec[n - 1]);
	b.pb(vec[n - 2]);
	while (l <= r)
	{
		ll y = vec[r];
		ll q, w;
		bool aa, bb;
		if (abs(a[a.size() - 1] - y) > abs(b[b.size() - 1] - y))
		{
			q = abs(a[a.size() - 1] - y);
			aa = true;
		}
		else
		{
			q = abs(b[b.size() - 1] - y);
			aa = false;
		}

		ll u = vec[l];
		if (abs(a[a.size() - 1] - u) > abs(b[b.size() - 1] - u))
		{
			w = abs(a[a.size() - 1] - u);
			bb = true;
		}
		else
		{
			w = abs(b[b.size() - 1] - u);
			bb = false;
		}
		if (w > q)
		{
			l++;
			if (bb)
			{
				a.pb(u);
			}
			else
			{
				b.pb(u);
			}
		}
		else
		{
			r--;
			if (aa)
			{
				a.pb(y);
			}
			else
			{
				b.pb(y);
			}
		}
	}
	ll rr = 0;
	for (ll i = a.size() - 1; i > 0; i--)
	{
		rr += abs(a[i] - a[i - 1]);
	}
	rr += abs(a[0] - b[0]);
	for (ll i = 1; i < b.size(); i++)
	{
		rr += abs(b[i] - b[i - 1]);
	}
	return rr;
}

ll maxim() {
	ll l = 2, r = n - 2;
	bool e = true;
	a1.pb(vec[n - 1]);
	a1.pb(vec[0]);
	b1.pb(vec[1]);
	while (l <= r)
	{
		ll y = vec[r];
		ll q, w;
		bool aa, bb;
		if (abs(a1[a1.size() - 1] - y) > abs(b1[b1.size() - 1] - y))
		{
			q = abs(a1[a1.size() - 1] - y);
			aa = true;
		}
		else
		{
			q = abs(b1[b1.size() - 1] - y);
			aa = false;
		}

		ll u = vec[l];
		if (abs(a1[a1.size() - 1] - u) > abs(b1[b1.size() - 1] - u))
		{
			w = abs(a1[a1.size() - 1] - u);
			bb = true;
		}
		else
		{
			w = abs(b1[b1.size() - 1] - u);
			bb = false;
		}
		if (w > q)
		{
			l++;
			if (bb)
			{
				a1.pb(u);
			}
			else
			{
				b1.pb(u);
			}
		}
		else
		{
			r--;
			if (aa)
			{
				a1.pb(y);
			}
			else
			{
				b1.pb(y);
			}
		}
	}
	ll rr = 0;
	for (ll i = a1.size() - 1; i > 0; i--)
	{
		rr += abs(a1[i] - a1[i - 1]);
	}
	rr += abs(a1[0] - b1[0]);
	for (ll i = 1; i < b1.size(); i++)
	{
		rr += abs(b1[i] - b1[i - 1]);
	}
	return rr;
}

int main()
{
	cin >> n;
	vec.resize(n);
	for (ll i = 0; i < n; i++)
	{ 
		cin >> vec[i];
	}
	if (n == 2)
	{
		cout << max(vec[0] - vec[1], vec[1] - vec[0]);
	}
	sort(vec.begin(), vec.end());
	ll r1 = maxim();
	ll r2 = minim();
	cout << max(r1, r2);
	return 0;
}