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: Wrong Answer)


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) {

		int N;
		int[] A;
		int[] sortPattern1;
		int[] sortPattern2;
		int sa;
		int saGoukei1 = 0;
		int saGoukei2 = 0;

		try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
			N = Integer.valueOf(reader.readLine());
			A = new int[N];
			for (int i = 0; i < N; i++) {
				A[i] = Integer.valueOf(reader.readLine());
				if (i >= 1) {
					for (int j = i; j > 0; j--) {
						if (A[j - 1] > A[j]) {
							int temp = A[j];
							A[j] = A[j - 1];
							A[j - 1] = temp;
						}
					}
				}
			}

			int midIdx;
			if (N % 2 == 0)
				midIdx = N / 2 - 1;
			else
				midIdx = N / 2;

			sortPattern1 = new int[N];
			sortPattern1[0] = A[midIdx];

			boolean largeSideFlg = true;
			int counter1 = 1;
			int sortCounter = 1;
			// 差分が最大になるようにソート
			for (int i = N - counter1; i != midIdx;) {
				sortPattern1[sortCounter] = A[i];
				sa = sortPattern1[sortCounter - 1] - sortPattern1[sortCounter];
				if (sa < 0)
					sa = -sa;
				saGoukei1 += sa;
				sortCounter++;
				if (largeSideFlg) {
					i = counter1 - 1;
					largeSideFlg = false;
				} else {
					counter1++;
					largeSideFlg = true;
					i = N - counter1;
				}
			}

			//			sortPattern2 = new int[N];
			//			sortPattern2[0] = A[midIdx];
			//
			//			boolean smallSideFlg = true;
			//			int counter2 = 0;
			//			int sortCounter2 = 1;
			//			// 差分が最大になるようにソート
			//			for (int i = counter2; sortCounter2 != N;) {
			//				sortPattern2[sortCounter2] = A[i];
			//				sortCounter2++;
			//				if (smallSideFlg) {
			//					smallSideFlg = false;
			//					i = N - counter2 - 1;
			//				} else {
			//					counter2++;
			//					i = counter2;
			//					smallSideFlg = true;
			//				}
			//			}
			//
			//			for (int i = 0; i < N - 1; i++) {
			//				sa = sortPattern2[i] - sortPattern2[i + 1];
			//				if (sa < 0)
			//					sa = -sa;
			//				saGoukei2 += sa;
			//			}
			//
			//			if (saGoukei1 > saGoukei2)
			System.out.println(saGoukei1);
			//			else
			//				System.out.println(saGoukei2);

		} catch (Exception e) {
			System.out.println(e);

		} finally {

		}

	}

}