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 (Clang 3.8.0) (judge verdict: Accepted)

#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

template<typename T1, typename T2>
inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }

template<typename T1, typename T2>
inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//ここから編集する

bool largest(ll a, ll b, ll c, ll d) {
    return a >= b && a >= c && a >= d;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    vector<ll> A(N);
    rep(i, 0, N) cin >> A[i];

    sort(all(A));
    if (N <= 2) {
        cout << abs(A[1] - A[0]) << endl;
        return 0;
    }
    if (N <= 3) {
        sort(all(A));
        ll kouho1 = abs(A[2] - A[0]) + abs(A[1] - A[0]);
        ll kouho2 = abs(A[0] - A[2]) + abs(A[1] - A[2]);
        cout << max(kouho1,kouho2) << endl;
        return 0;
    }


    ll C = A[0];
    vector<ll> L, R;

    L.push_back(A[N - 1]);
    R.push_back(A[N - 2]);

    int lpt = 1;
    int rpt = N - 3;

    while (lpt <= rpt) {
        ll val1 = abs(L.back() - A[lpt]);
        ll val2 = abs(L.back() - A[rpt]);
        ll val3 = abs(R.back() - A[lpt]);
        ll val4 = abs(R.back() - A[rpt]);

        if (largest(val1, val2, val3, val4)) {
            L.push_back(A[lpt]);
            lpt++;
        } else if (largest(val2, val1, val3, val4)) {
            L.push_back(A[rpt]);
            rpt--;
        } else if (largest(val3, val1, val2, val4)) {
            R.push_back(A[lpt]);
            lpt++;
        } else if (largest(val4, val1, val2, val3)) {
            R.push_back(A[rpt]);
            rpt--;
        } else {
            return -1;
        }
    }

    vector<ll> arr;
    for (int i = L.size() - 1; i >= 0; --i) {
        arr.push_back(L[i]);
    }
    arr.push_back(C);
    for (int i = 0; i < R.size(); ++i) {
        arr.push_back(R[i]);
    }

    ll ans = 0ll;
    for (int i = 0; i < arr.size() - 1; ++i) {
        ans += abs(arr[i + 1] - arr[i]);
    }

    cout << ans << endl;


    return 0;
}