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: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            SolveA();
            //SolveB();
            //SolveC();
            //SolveD();
        }
        private const long MOD = 1000000007;
        static void SolveA()
        {
            int n = ReadAndParseInt();
            long[] a = new long[n];
            for(int i=0;i<n;++i)
            {
                a[i] = ReadAndParseLong();
            }
            var alist = new List<long>(a);
            alist.Sort();

            long x0 = CalcMax(n, 0, alist);
            long x1 = CalcMax(n, 1, alist);
            long ans = Math.Max(x0, x1);

            Console.WriteLine(ans);
        }

        private static long CalcMax(int n, int minusStartIdx, List<long> alist)
        {
            var list = new List<int>(CalcCoefArr(n, minusStartIdx));
            list.Sort();

            long ans = 0;
            for (int i = 0; i < n; ++i)
            {
                ans += alist[i] * list[i];
            }
            return ans;
        }

        private static int[] CalcCoefArr(int n, int minusStartIdx)
        {
            var coefArr = new int[n];
            coefArr[0] = 1;
            coefArr[n - 1] = 1;
            for(int i=1;i<n-1;++i)
            {
                coefArr[i] = 2;
            }
            for(int i=minusStartIdx;i<n;i+=2)
            {
                coefArr[i] *= -1;
            }
            return coefArr;
        }
        #region よく使う便利関数
        private static bool isPrime(long x)
        {
            if( x == 2) { return true; }
            if( x < 2 || x % 2 == 0) { return false; }
            long i = 3;
            while( i * i <= x)
            {
                if( x % i == 0) { return false; }
                i = i + 2;
            }
            return true;
        }
        private static long lcm(long x, long y)
        {
            return x / gcd(x, y) * y;
        }
        private static long gcd(long x, long y)
        {
            return y > 0 ? gcd(y, x % y) : x;
        }

        private static int ReadAndParseInt()
        {
            return int.Parse(Console.ReadLine());
        }
        private static int[] ReadAndParseIntArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        }
        private static long ReadAndParseLong()
        {
            return long.Parse(Console.ReadLine());
        }
        private static long[] ReadAndParseLongArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        }
        #endregion
    }
}