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>

using namespace std;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define int ll
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector< vi > vvi;
typedef vector< vvi > vvvi;
typedef vector<short> vs;
typedef vector<vs> vvs;
typedef vector<vvs> vvvs;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<vvld> vvvld;
typedef vector<string> vst;
typedef vector<vst> vvst;
typedef pair<ld, ld> pld;
typedef complex<double> base;

#define mmin(a, b) a = min(a, (b))
#define mmax(a, b) a = max(a, (b))
#define mp(a,b) make_pair(a,b)
#define modsum(a, b) ((a) + (b) >= M ? (a) + (b) - M : (a) + (b))
#define modsubtract(a, b) ((a) - (b) < 0 ? (a) - (b) + M : (a) - (b))
#define modmultiply(a, b) (((a) * (b)) % M)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define sqr(x) ((x) * (x))
#define fori(i, n) for(int i = 0; i < int(n); ++i)
#define cint const int &
#define SZ(a) ((int)(a.size()))
#define watch(x) cerr << (#x) << " = " << x << endl;

const double PI = 2 * acos(0.0);

const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";


template<class T>
ostream & operator << (ostream &out, vector<T> &a) {
    out << "{";
    for (auto x : a) out << x << " ";
    return out << "}";
}

template<class T>
ostream & operator << (ostream &out, pair<T, T> &a) {
    out << "{";
    cout << a.first << ", " << a.second;
    return out << "}";
}

void smain();

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

#ifdef MAX_HOME
    freopen("input.txt", "r", stdin);
#endif

    cout << setprecision(12) << fixed;

    smain();
}

//
//const int N = 309;
//
//ll solve(vvi mat, int dist) {
//    int n = mat.size(), m = mat[0].size();
//    vvl cnt(n, vl(m, 0));
//    vvl a = calc(mat, dist);
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//
//    vvl tmp = inverse_i(mat);
//    a = inverse_i(calc(tmp, dist));
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//
//    tmp = inverse_j(mat);
//    a = inverse_j(calc(tmp, dist));
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//
//    tmp = inverse_i(inverse_j(mat));
//    a = inverse_i(inverse_j(calc(tmp, dist)));
//    fori (i, n) fori (j, m) cnt[i][j] += a[i][j];
//    ll ret = 0;
//    fori (i, n) fori (j, n) {
//        ll cur = a[i][j];
//        ret = cur * (cur - 1) * (cur - 2) / (2LL * 3LL);
//    }
//    return ret;
//}

ll solve1(int n, vi a) {
    int h = n >> 1;
    int h2 = n - h;
    sort(RALL(a));
    vi b;
    vi tmp;
    for (int i = 0; i < n; ++i) if (i < h) b.push_back(a[i]);
        else tmp.push_back(a[i]);
    a = tmp;
    vi res(n);

    res[0] = a[0];
    if (a.size() >= 2) res[n - 1] = a[1];
    int i = 0;
    int j = 2;
    for (int k = 1; k < n; ++k) {
        if (k & 1) res[k] = b[i++];
        else res[k] = a[j++];
    }
    ll dif = 0;
    for (int i = 1; i < n; ++i) dif += abs(res[i] - res[i - 1]);
    watch(res);
    return dif;
}

ll solve2(int n, vi a) {
    int h = n >> 1;
    int h2 = n - h;
    sort(ALL(a));
    vi b;
    vi tmp;
    for (int i = 0; i < n; ++i) if (i < h) b.push_back(a[i]);
        else tmp.push_back(a[i]);
    a = tmp;
    vi res(n);

    res[0] = a[0];
    if (a.size() >= 2) res[n - 1] = a[1];
    int i = 0;
    int j = 2;
    for (int k = 1; k < n; ++k) {
        if (k & 1) res[k] = b[i++];
        else res[k] = a[j++];
    }
    ll dif = 0;
    for (int i = 1; i < n; ++i) dif += abs(res[i] - res[i - 1]);
    return dif;
}

ll solve3(int n, vi a) {
    int h = n >> 1;
    int h2 = n - h;
    sort(ALL(a));
    vi b;
    vi tmp;
    for (int i = 0; i < n; ++i) if (i < h) b.push_back(a[i]);
        else tmp.push_back(a[i]);
    a = tmp;
    vi res(n);
    sort(ALL(a));
    sort(ALL(b));
    res[0] = a.back();
    res[n - 1] = b[0];
    int i = 0;
    int j = 1;
    for (int k = 1; k < n - 1; ++k) {
        if (k & 1) {
            res[k] = b[j++];
        } else res[k] = a[i++];
    }
    ll dif = 0;
    for (int i = 1; i < n; ++i) dif += abs(res[i] - res[i - 1]);
    return dif;
}

void smain() {
    int n;
    cin >> n;
    vi a(n);
    fori (i, n) cin >> a[i];
    if (n & 1)
        cout << max(solve1(n, a), solve2(n, a));
    else
        cout << solve3(n, a);
}
//
//void smain2() {
//    int n, m;
//    cin >> n >> m;
//    vvi mat(n, vi(m, 0));
//
//    fori (i, n) {
//        string s;
//        cin >> s;
//        fori (j, m) mat[i][j] = s[j] == '#';
//    }
//
//    ll ans = 0;
//
//    for (int i = 1; i <= n; ++i)  ans += solve(mat, i);
//    cout << ans;
//}
//