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 Atcoder;
using System.Threading.Tasks;
using System.ComponentModel;

public class Hello
{

    public static void Main()
    {
        int N = intRead();
        List<int> Ai = new List<int>(N);
        List<bool> Ai_b = new List<bool>(N);
        for (int i = 0; i < N; i++) Ai_b.Add(true);
        Ai_b[0] = false;
        for (int i = 0; i < N; i++) Ai.Add(intRead());
        Ai.Sort();
        //小さい順
        int Left_num = Ai[0];
        int Right_num = Ai[0];
        int catch_num = 0;
        int ans = 0;

        for (int i = 0; i < N - 1; i++)
        {

            for (int j = (i % 2 == 0 ? N - 1 : 0); j != (i % 2 == 0 ? -1 : N-1); j += (i % 2 == 0 ? -1 : 1))
            {
                if (Ai_b[j])
                {
                    Ai_b[j] = false;
                    catch_num = Ai[j];
                    break;
                }
            }
            //Console.WriteLine(catch_num);
            if (Math.Abs(catch_num - Left_num) < Math.Abs(catch_num - Right_num))
            {
                ans += Math.Abs(catch_num - Right_num);
                Right_num = catch_num;
            }
            else
            {
                ans += Math.Abs(catch_num - Left_num);
                Left_num = catch_num;
                
            }
        }

        Console.WriteLine(ans);
    }
}


public class Atcoder
{
    static void Swap<T>(ref T a, ref T b)
    {
        var t = a;
        a = b;
        b = t;
    }
    public static long LCM(long a, long b)
    {
        long c = a * b;
        if (a < b) Swap(ref a, ref b);
        long d = a % b;
        while (d != 0)
        {
            a = b;
            b = d;
            d = a % b;
        }
        return c / b;
    }
    public static string strReverce(string s)
    {
        return new string(s.Reverse().ToArray());
    }
    public static int intRead()
    {
        return int.Parse(Console.ReadLine());
    }
    public static bool IsPrime(int n)
    {
        if (n < 2) return false;
        if (n == 2) return true;
        if (n % 2 == 0) return false;

        //double sqrtNum = Math.Sqrt(n);
        for (int i = 3; i < n; i += 2)
        {
            if (n % i == 0) return false;
        }
        return true;
    }
    public static string[] SpRead(char c)
    {
        return Console.ReadLine().Split(c);
    }
    public static int[] SpIntRead(char c)
    {
        return Console.ReadLine().Split(' ').Select(a => int.Parse(a)).ToArray();
    }
    public static string ToConvertBit(int N, int e)
    {
        string ret = "";
        int r = 0;
        while (N != 0)
        {
            r = N % e;
            ret += r.ToString();
            N -= r;
            N /= e;
        }
        ret = strReverce(ret);
        return ret;
    }

}