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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

//package test;
import java.util.*;
public class Main {
	private static int n;
	private static final int  maxn=100000+10;
	private static int[] a=new int[maxn];
	private static int[] b=new int[maxn];
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		n=scan.nextInt();
		for(int i=1;i<=n;i++)
			a[i]=scan.nextInt();
		quickSort(a,1,n);
		long ans=works();
		for(int i=1,t;i*2<=n;i++) {
			t=a[i];a[i]=a[n-i+1];a[n-i+1]=t;
		}
		System.out.println(Math.max(ans, works()));
		return;
	}
	static void quickSort(int array[], int low, int high) {// 传入low=0，high=array.length-1;
		int pivot, p_pos, i, t;// pivot->位索引;p_pos->轴值。
		if (low < high) {
			p_pos = low;
			pivot = array[p_pos];
			for (i = low + 1; i <= high; i++)
				if (array[i] > pivot) {
					p_pos++;
					t = array[p_pos];
					array[p_pos] = array[i];
					array[i] = t;
				}
			t = array[low];
			array[low] = array[p_pos];
			array[p_pos] = t;
			// 分而治之
			quickSort(array, low, p_pos - 1);// 排序左半部分
			quickSort(array, p_pos + 1, high);// 排序右半部分
		}
	}
	static long works() {
		int i,j;initb();
		i=1;j=(n+1)/2;
		b[1]=a[j--];
		if(n%2>0) b[n]=a[j--];
		for(;i<=n;i+=2)
			if(b[i]==0) b[i]=a[j--];
		for(i=n%2>0?n-1:n,j=(n+1)/2+1;i>0;i-=2)
			if(b[i]==0) b[i]=a[j++];
		return count();
	}
	static long count() {
		long val=0;
		for(int i=2;i<=n;i++)
			val+=Math.abs(b[i]-b[i-1]);
		return val;
	}
	static void initb() {
		for(int i=1;i<=n;i++)
			b[i]=0;
		return;
	}
}