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;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = ReadInt();
            var A = new List<int>();
            for (int i = 0; i < N; i++) A.Add(ReadInt());
            A.Sort();
            var Asor = new List<int>();
            Asor.Add(A[0]);
            A.RemoveAt(0);
            Asor.Add(A[A.Count - 1]);
            A.RemoveAt(A.Count - 1);

            while (A.Count > 0)
            {
                if (A.Count == 1)
                {
                    var front = Math.Abs(A[0] - Asor[0]);
                    var back = Math.Abs(Asor[A.Count - 1] - A[0]);
                    if (front >= back)
                    {
                        Asor.Insert(0, A[0]);
                        A.RemoveAt(0);
                    }else
                    {
                        Asor.Add(A[0]);
                        A.RemoveAt(0);
                    }
                    break;
                }
                var first = A[0];
                var last = A[A.Count - 1];
                var firstFront = Math.Abs(first - Asor[0]);
                var backFirst = Math.Abs(Asor[Asor.Count - 1] - first);
                var lastFront = Math.Abs(last - Asor[0]);
                var backLast = Math.Abs(Asor[Asor.Count - 1] - last);
                if (firstFront >= backFirst && firstFront >= lastFront && firstFront >= backLast)
                {
                    Asor.Insert(0, first);
                    A.RemoveAt(0);
                }
                else if (backFirst >= firstFront && backFirst >= lastFront && backFirst >= backLast)
                {
                    Asor.Add(first);
                    A.RemoveAt(0);
                }
                else if (lastFront >= firstFront && lastFront >= backFirst && lastFront >= backLast)
                {
                    Asor.Insert(0, last);
                    A.RemoveAt(A.Count - 1);
                }
                else
                {
                    Asor.Add(last);
                    A.RemoveAt(A.Count - 1);
                }
            }

            var ans = 0L;
            for (int i = 0; i < Asor.Count - 1; i++) ans += Math.Abs(Asor[i + 1] - Asor[i]);

            Console.WriteLine(ans);
            Console.ReadLine();
        }

        static string Read() { return Console.ReadLine(); }
        static int ReadInt() { return int.Parse(Read()); }
        static long ReadLong() { return long.Parse(Read()); }
        static int[] ReadArrayInt() { return Read().Split(' ').Select(s => int.Parse(s)).ToArray(); }
        static long[] ReadArrayLong() { return Read().Split(' ').Select(s => long.Parse(s)).ToArray(); }
    }
}