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 <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
#include <cmath>
//#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>

const int MAX_N = 100050;

long long A[MAX_N];
long long sgn1[MAX_N];
long long sgn2[MAX_N];
int N;

int main(int argc, char **argv) {
    std::cin >> N;

    for (int i = 1; i <= N; i++) {
        std::cin >> A[i];
    }

    for (int i = 1; i <= N; i++) {
        sgn1[i] = 0;
        sgn2[i] = 0;
    }

    for (int i = 2; i <= N; i++) {
        if (i % 2 == 0) {
            sgn1[i-1]++;
            sgn1[i]--;

            sgn2[i-1]--;
            sgn2[i]++;
        } else {
            sgn1[i-1]--;
            sgn1[i]++;

            sgn2[i-1]++;
            sgn2[i]--;
        }
    }

    std::vector<long long> vec, sgn1_vec, sgn2_vec;

    for (int i = 1; i <= N; i++) {
        vec.push_back(A[i]);
        sgn1_vec.push_back(sgn1[i]);
        sgn2_vec.push_back(sgn2[i]);
    }

    std::sort(vec.begin(), vec.end());
    std::sort(sgn1_vec.begin(), sgn1_vec.end());
    std::sort(sgn2_vec.begin(), sgn2_vec.end());

    long long ret1 = 0;
    long long ret2 = 0;
    for (int i = 0; i < N; i++) {
        ret1 += sgn1_vec[i]*vec[i];
        ret2 += sgn2_vec[i]*vec[i];
    }

    std::cout << std::max(ret1, ret2) << std::endl;

    return 0;
}