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 System.Text;

namespace C_Align
{
    class Program
    {
        static void Main(string[] args)
        {
            var N = int.Parse(Console.ReadLine());
            var AList = new List<int>();
            for (int i = 0; i < N; i++)
            {
                var A = int.Parse(Console.ReadLine());
                AList.Add(A);
            }

            ulong difMax = 0;
            var p = new Permutations<int>(AList);
            foreach (var perm in p)
            {
                ulong difSum = 0;
                for (int i = 0; i < (perm.Count()-1); i++)
                {
                    difSum = difSum + (ulong)Math.Abs(perm[i] - perm[i+1]);
                }
                difMax = Math.Max(difMax, difSum);
            }

            Console.WriteLine(difMax);
        }


    }
    public class Permutations<T>
    {
        #region メンバ
        private IList<T> _items;
        private int _length;
        private List<IList<T>> _permutations;
        private List<int[]> _indices;
        private int[] _value;
        private int _level = -1;
        #endregion

        #region コンストラクタ
        public Permutations(IList<T> items) : this(items, items.Count) { }
        public Permutations(IList<T> items, int select)
        {
            if (select < 0)
                throw new ArgumentException("選択要素数は、0以上でなくてはなりません。");
            if (items.Count < select)
                throw new ArgumentException("選択要素数は、対象要素数以下でなくてはなりません。");

            _items = items;
            _length = select;
            _permutations = new List<IList<T>>();
            _indices = new List<int[]>();

            if (select == 0) return;

            _value = new int[_length];
            CreateCombinationIndices(0);

            foreach (var oneCom in new Combinations<T>(items, select))
                _permutations.AddRange(GetPermutations(oneCom));
        }
        #endregion

        #region プロパティ
        /// <summary>
        /// 順列の要素数を取得します。
        /// </summary>
        public int Count
        {
            get { return _permutations.Count != 0 ? _permutations.Count : 1; }
        }
        #endregion

        #region メソッド
        /// <summary>
        /// 組み合わせに対する順列インデックスを生成
        /// </summary>
        /// <param name="i"></param>
        private void CreateCombinationIndices(int i)
        {
            try
            {
                _value[i] = ++_level;

                if (_level != _length)
                {
                    for (int j = 0; j < _length; j++)
                        if (_value[j] == 0) CreateCombinationIndices(j);
                    return;
                }
                int[] newValue = _value.Clone() as int[];
                _indices.Add(newValue);
            }
            finally
            {
                _level--;
                _value[i] = 0;
            }
        }

        /// <summary>
        /// 順列を生成
        /// </summary>
        /// <param name="oneCom"></param>
        /// <returns></returns>
        private IEnumerable<IList<T>> GetPermutations(IList<T> oneCom)
        {
            foreach (var idxs in _indices)
            {
                T[] onePerm = new T[_length];
                for (int i = 0; i < _length; i++)
                {
                    onePerm[i] = oneCom[idxs[i] - 1];
                }
                yield return onePerm;
            }
        }

        /// <summary>
        /// 結果を返すイテレータ
        /// </summary>
        /// <returns></returns>
        public IEnumerator<IList<T>> GetEnumerator()
        {
            foreach (var item in _permutations) yield return item;
        }

        /// <summary>
        /// ToString
        /// </summary>
        /// <returns></returns>
        //public override string ToString()
        //{
        //    return string.Format("{0}P{1} Of [{2}] - {3}通り"
        //        , _items.Count
        //        , _length
        //        , Utility.GetString<T>(_items)
        //        , this.Count);
        //}
        #endregion
    }

    /// <summary>
    /// 組み合わせ
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Combinations<T>
    {
        #region メンバ
        private IList<T> _items;
        private int _length;
        private List<IList<T>> _combinations;
        private int[] _finalIndices;
        #endregion

        #region コンストラクタ
        public Combinations(IList<T> items) : this(items, items.Count) { }
        public Combinations(IList<T> items, int select)
        {
            if (select < 0)
                throw new ArgumentException("選択要素数は、0以上でなくてはなりません。");
            if (items.Count < select)
                throw new ArgumentException("選択要素数は、対象要素数以下でなくてはなりません。");

            _items = items;
            _length = select;
            _combinations = new List<IList<T>>();

            if (select == 0) return;

            _finalIndices = GetFinalIndices(select);
            ComputeCombination();
        }
        #endregion

        /// <summary>
        /// 選択要素数に応じた組み合わせとなる最後の値を取得します。(終端の確定)
        /// </summary>
        /// <param name="select"></param>
        /// <returns></returns>
        private int[] GetFinalIndices(int select)
        {
            var finalindices = new int[select];

            int j = select - 1;
            for (var i = _items.Count - 1; i > _items.Count - 1 - select; i--)
            {
                finalindices[j] = i;
                j--;
            }
            return finalindices;
        }

        #region プロパティ
        /// <summary>
        /// 組み合わせの要素数を取得します。
        /// </summary>
        public int Count
        {
            get { return _combinations.Count != 0 ? _combinations.Count : 1; }
        }
        #endregion

        /// <summary>
        /// 条件をもとに組み合わせを求めます。
        /// </summary>
        private void ComputeCombination()
        {
            if (_finalIndices.Length == 0) return;

            var indices = Enumerable.Range(0, _length).ToArray();
            _combinations.Add(GetCurrentCombination(indices));

            while (NextCombination(indices))
            {
                _combinations.Add(GetCurrentCombination(indices));
            }
        }

        /// <summary>
        /// 現在の組み合わせを取得します。
        /// </summary>
        /// <param name="indices"></param>
        /// <returns></returns>
        private T[] GetCurrentCombination(int[] indices)
        {
            T[] oneCom = new T[_length];
            for (var k = 0; k < _length; k++)
            {
                oneCom[k] = _items[indices[k]];
            }
            return oneCom;
        }

        /// <summary>
        /// 次の組み合わせへ
        /// </summary>
        /// <param name="indices"></param>
        /// <returns></returns>
        private bool NextCombination(int[] indices)
        {
            for (var j = _finalIndices.Length - 1; j > -1; j--)
            {
                if (indices[j] < _finalIndices[j])
                {
                    indices[j]++;
                    for (var k = 1; j + k < _finalIndices.Length; k++)
                    {
                        indices[j + k] = indices[j] + k;
                    }
                    break;
                }
                if (j == 0) return false;
            }
            return true;
        }

        /// <summary>
        /// イテレータ
        /// </summary>
        /// <returns></returns>
        public IEnumerator<IList<T>> GetEnumerator()
        {
            foreach (var item in _combinations) yield return item;
        }

        ///// <summary>
        ///// ToString
        ///// </summary>
        ///// <returns></returns>
        //public override string ToString()
        //{
        //    return string.Format("{0}C{1} Of [{2}] - {3}通り"
        //            , _items.Count
        //                , _length
        //                , Utility.GetString<T>(_items)
        //                , this.Count);
        //}
    }
}