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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using static Input;

public class Prog
{
    private const int INF = 1000000007;
    private const long INFINITY = 9223372036854775807;
    private static int[] make_cost = { 0, 2, 5, 5, 4, 5, 6, 3, 7, 6 };
    public static void Main()
    {
        int N = NextInt();
        int[] A = new int[N];
        for (int i = 0; i < N; i++) A[i] = NextInt();

        Array.Sort(A);

        //大きいほうから
        int L = A[N - 1];
        int R = A[N - 1];
        int max_index = N - 2;
        int min_index = 0;
        long ans1 = 0;
        for (int i = 1; i < N; i++)
        {
            if (i % 4 == 1 || i % 4 == 2)
            {
                //小さいの付与
                int _L = Math.Abs(L - A[min_index]);
                int _R = Math.Abs(R - A[min_index]);
                ans1 += Math.Max(_L, _R);
                if (_L > _R) L = A[min_index];
                else R = A[min_index];
                min_index++;
            }
            else if (i % 4 == 3 || i % 4 == 0)
            {
                //大きいの
                int _L = Math.Abs(L - A[max_index]);
                int _R = Math.Abs(R - A[max_index]);
                ans1 += Math.Max(_L, _R);
                if (_L > _R) L = A[max_index];
                else R = A[max_index];
                max_index--;
            }
        }

        L = A[0];
        R = A[0];
        max_index = N - 1;
        min_index = 1;
        long ans2 = 0;
        for (int i = 1; i < N; i++)
        {
            if (i % 4 == 3 || i % 4 == 0)
            {
                //小さいの付与
                int _L = Math.Abs(L - A[min_index]);
                int _R = Math.Abs(R - A[min_index]);
                ans2 += Math.Max(_L, _R);
                if (_L > _R) L = A[min_index];
                else R = A[min_index];
                min_index++;
            }
            else if (i % 4 == 1 || i % 4 == 2)
            {
                //大きいの
                int _L = Math.Abs(L - A[max_index]);
                int _R = Math.Abs(R - A[max_index]);
                ans2 += Math.Max(_L, _R);
                if (_L > _R) L = A[max_index];
                else R = A[max_index];
                max_index--;
            }
        }

        Console.WriteLine(Math.Max(ans1, ans2));
    }
}

public class Input
{
    private static Queue<string> q = new Queue<string>();
    private static void Confirm() { if (q.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) q.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(q.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(q.Dequeue()); }
    public static string NextString() { Confirm(); return q.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(q.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}