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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		Array.Sort(A);
		
		
		var dq = new Deque<long>(1000000);
		dq.AddFirst(A[0]);
		
		int l = 1;
		int r = N - 1;
		while(dq.Count < N){
			long la = A[l];
			long ra = A[r];
			
			long ma = 0;
			int idx = -1;
			for(int i=0;i<4;i++){
				var v = 0L;
				if(i < 2) {
					v = la;
				} else {
					v = ra;
				}
				var w = 0L;
				if(i % 2 == 0){
					w = dq.First;
				} else {
					w = dq.Last;
				}
				
				var vw = Math.Abs(v - w);
				if(vw > ma){
					ma = vw;
					idx = i;
				}
			}
			
			switch(idx){
				
				case 0: {dq.AddFirst(la);l++;} break;
				case 1: {dq.AddLast(la);l++;} break;
				case 2: {dq.AddFirst(ra);r--;}; break;
				case 3: {dq.AddLast(ra);r--;}; break;
				
			}
		}
		
		var L = new List<long>();
		while(dq.Count > 0){
			L.Add(dq.First);
			dq.RemoveFirst();
		}
		
		long ans = 0;
		for(int i=0;i<N-1;i++) ans += Math.Abs(L[i] - L[i+1]);
		Console.WriteLine(ans);
		
	}
	int N;
	long[] A;
	public Sol(){
		N = ri();
		A = new long[N];
		for(int i=0;i<N;i++) A[i] = rl();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}




class Deque<T> :IEnumerable<T>{
	
	
	T[] arr;
	int head, tail;
	int count;
	int capacity;
	
	public int Capacity{
		get{ return capacity;}
	}
	public int Count{
		get{ return count;}
	}
	
	public T First {
		get { 
			if(count == 0) throw new Exception("empty");
			return arr[head];
		}
	}
	public T Last {
		get { 
			if(count == 0) throw new Exception("empty");
			return arr[tail]; 
		}
	}
	
	public T this [int pos]{
		get{
			if(pos >= count) throw new ArgumentOutOfRangeException("out of range");
			return arr[(head + pos) % arr.Length];
		}
	}
	
	public IEnumerator<T> GetEnumerator(){
		for(int i=0;i<count;i++){
			yield return arr[(head + i) % arr.Length];
		}
	}
	
	IEnumerator IEnumerable.GetEnumerator() {
		return this.GetEnumerator();
	}
		
	public void AddFirst(T elem){
		if(count == capacity) {
			Twice();
		}
		
		head = count == 0 ? head : (head - 1);
		if(head < 0) head = arr.Length - 1;
		arr[head] = elem;
		count++;
		
	}
	
	public void RemoveFirst(){
		if(count == 0) throw new Exception("empty");
		arr[head] = default(T);
		count--;
		head = count == 0 ? head : head + 1; if(head == arr.Length) head = 0;
	}
	
	public void AddLast(T elem){
		if(count == capacity) {
			Twice();
		}
		
		tail = count == 0 ? tail : (tail + 1);
		if(tail >= arr.Length) tail = 0;
		arr[tail] = elem;
		count++;
		
	}
	
	public void RemoveLast(){
		if(count == 0) throw new Exception("empty");
		arr[tail] = default(T);
		count--;
		tail = count == 0 ? tail : tail - 1; if(tail < 0) tail = arr.Length - 1;
	}
	
	void Twice(){
		
		T[] nxt = new T[arr.Length * 2];
		int nhead = arr.Length / 2;
		for(int i = 0; i < count; i++) nxt[nhead + i] = arr[(head + i) % arr.Length];
		head = nhead;
		tail = head + count - 1;
		arr = nxt; 
		capacity = arr.Length;
	}
	
	public Deque() {
		Init(256);
	}
	
	public Deque(int cap_){
		Init(cap_);
	}
	
	void Init(int cap){
		if(cap < 0) cap = 256;
		arr = new T[cap];
		capacity = cap;
		head = cap / 2;
		tail = cap / 2;
		count = 0;
	}
	
	
}