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: Time Limit Exceeded)

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;
    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[] _A = new int[N];
        _A = A.Reverse().ToArray();
        Queue<int> pt1 = new Queue<int>();
        pt1.Enqueue(A[N - 1]);
        int min_ind = 0;
        int max_ind = 1;
        for (int i = 1; i < N; i++)
        {
            if (i % 4 == 1 || i % 4 == 2) { pt1.Enqueue(A[min_ind]); min_ind++; }
            else if (i % 4 == 3 || i % 4 == 0) { pt1.Enqueue(_A[max_ind]); max_ind++; }
        }
        List<int> list1 = new List<int>();
        list1.Add(pt1.Dequeue());
        while (pt1.Count() != 0)
        {
            int a = pt1.Dequeue();
            if (Math.Abs(list1.First() - a) < Math.Abs(list1.Last() - a)) { list1.Add(a); }
            else { list1.Reverse(); list1.Add(a); }
        }
        long ans1 = 0;
        for (int i = 1; i < N; i++) ans1 += Math.Abs(list1[i - 1] - list1[i]);

        Queue<int> pt2 = new Queue<int>();
        pt2.Enqueue(A[0]);
        min_ind = 1;
        max_ind = 0;
        for (int i = 1; i < N; i++)
        {
            if (i % 4 == 3 || i % 4 == 0) { pt2.Enqueue(A[min_ind]); min_ind++; }
            else if (i % 4 == 1 || i % 4 == 2) { pt2.Enqueue(_A[max_ind]); max_ind++; }
        }
        List<int> list2 = new List<int>();
        list2.Add(pt2.Dequeue());
        while (pt2.Count() != 0)
        {
            int a = pt2.Dequeue();
            if (Math.Abs(list2.First() - a) < Math.Abs(list2.Last() - a)) { list2.Add(a); }
            else { list2.Reverse(); list2.Add(a); }
        }
        long ans2 = 0;
        for (int i = 1; i < N; i++) ans2 += Math.Abs(list1[i - 1] - list1[i]);

        //list1.ForEach(v => Console.Write(v + " "));
        //Console.WriteLine();
        //list2.ForEach(v => Console.Write(v + " "));
        //Console.WriteLine();

        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(); }
}