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.util.*;
import java.math.*;

public class Main {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		// 整数の入力
		int n = sc.nextInt();
		List<Integer> list = new ArrayList<>();
		for(int i=0;i<n;i++){
			list.add(sc.nextInt());
		}
		Collections.sort(list);
		
		int result = 0;

		int firstValue = list.get(0);
		int leftValue = firstValue;
		int rightValue =firstValue;
		int upperCount =n-1;
		int lowerCount =1;
		boolean preIsLeft =true;
		// System.out.println(list);
		

		for(int i=0;i<n-1;i++){
			switch (i%4) {
				case 0:
				int thisValue = list.get(upperCount);
				if(upperCount-lowerCount==0){
					if(Math.abs(leftValue-thisValue)>Math.abs(rightValue-thisValue)){
						result += Math.abs(leftValue-thisValue);
						leftValue = thisValue;
						
						upperCount--;
						preIsLeft =true;
					}else if(Math.abs(leftValue-thisValue)<Math.abs(rightValue-thisValue)){
						result += Math.abs(rightValue-thisValue);
						rightValue = thisValue;
						upperCount--;
						preIsLeft =false;
					}else{
						if(preIsLeft){
							result += Math.abs(rightValue-thisValue);
							rightValue = thisValue;
							upperCount--;
							preIsLeft =false;
						}else{
							result += Math.abs(rightValue-thisValue);
							rightValue = thisValue;
							upperCount--;
							preIsLeft =true;
						}
					}
					// System.out.println("left"+leftValue);
					// System.out.println("right"+rightValue);
					// System.out.println("result"+result);
					// System.out.println("thisValue"+thisValue);
					// System.out.println("preIsLeft"+preIsLeft);
					// System.out.println(" ");
					break;
				}

				result += Math.abs(leftValue-thisValue);
				leftValue = thisValue;
				upperCount--;
				preIsLeft =true;
				
				// System.out.println("left"+leftValue);
				// System.out.println("right"+rightValue);
				// System.out.println("result"+result);
				// System.out.println("thisValue"+thisValue);
				// System.out.println("preIsLeft"+preIsLeft);
				// System.out.println(" ");

				break;


				case 1:
					int oneValue = list.get(upperCount);
					if(upperCount-lowerCount==0){
						if(Math.abs(leftValue-oneValue)>Math.abs(rightValue-oneValue)){
							result += Math.abs(leftValue-oneValue);
							leftValue = oneValue;
							
							upperCount--;
							preIsLeft =true;
						}else if(Math.abs(leftValue-oneValue)<Math.abs(rightValue-oneValue)){
							result += Math.abs(rightValue-oneValue);
							rightValue = oneValue;
							upperCount--;
							preIsLeft =false;
						}else{
							if(preIsLeft){
								result += Math.abs(rightValue-oneValue);
								rightValue = oneValue;
								upperCount--;
								preIsLeft =false;
							}else{
								result += Math.abs(rightValue-oneValue);
								rightValue = oneValue;
								upperCount--;
								preIsLeft =true;
							}
						}
						// System.out.println("left"+leftValue);
						// System.out.println("right"+rightValue);
						// System.out.println("result"+result);
						// System.out.println("oneValue"+oneValue);
						// System.out.println("preIsLeft"+preIsLeft);
						// System.out.println(" ");
						break;
					}

					result += Math.abs(rightValue-oneValue);
					rightValue = oneValue;
					upperCount--;
					preIsLeft =false;
					
					// System.out.println("left"+leftValue);
					// System.out.println("right"+rightValue);
					// System.out.println("result"+result);
					// System.out.println("oneValue"+oneValue);
					// System.out.println("preIsLeft"+preIsLeft);
					// System.out.println(" ");

					break;
				case 2:
				int twoValue = list.get(lowerCount);
				if(upperCount-lowerCount==0){
					if(Math.abs(leftValue-twoValue)>Math.abs(rightValue-twoValue)){
						result += Math.abs(leftValue-twoValue);
						leftValue = twoValue;
						lowerCount++;
						preIsLeft =true;

					}else if(Math.abs(leftValue-twoValue)<Math.abs(rightValue-twoValue)){
						result += Math.abs(rightValue-twoValue);
						rightValue = twoValue;
						lowerCount++;
						preIsLeft =false;
					}else{
						if(preIsLeft){
							result += Math.abs(rightValue-twoValue);
							rightValue = twoValue;
							lowerCount++;
							preIsLeft =false;
						}else{
							result += Math.abs(leftValue-twoValue);
							leftValue = twoValue;
							lowerCount++;
							preIsLeft =true;
						}
					}

					// System.out.println("left"+leftValue);
					// System.out.println("right"+rightValue);
					// System.out.println("result"+result);
					// System.out.println("twoValue"+twoValue);
					// System.out.println("preIsLeft"+preIsLeft);
					// System.out.println(" ");
					break;
				}
				result += Math.abs(leftValue-twoValue);
						leftValue = twoValue;
						lowerCount++;
						preIsLeft =true;
						break;

				case 3:
				int threeValue = list.get(lowerCount);
				// System.out.println("threeValue"+threeValue);
				if(upperCount-lowerCount==0){
					// System.out.println("upperCount-lowerCount"+ (upperCount-lowerCount));
					if(Math.abs(leftValue-threeValue)>Math.abs(rightValue-threeValue)){
						result += Math.abs(leftValue-threeValue);
						leftValue = threeValue;
						lowerCount++;
						preIsLeft =true;

					}else if(Math.abs(leftValue-threeValue)<Math.abs(rightValue-threeValue)){
						result += Math.abs(rightValue-threeValue);
						rightValue = threeValue;
						lowerCount++;
						preIsLeft =false;
					}else{
						if(preIsLeft){
							result += Math.abs(rightValue-threeValue);
							rightValue = threeValue;
							lowerCount++;
							preIsLeft =false;
						}else{
							result += Math.abs(leftValue-threeValue);
							leftValue = threeValue;
							lowerCount++;
							preIsLeft =true;
						}
					}

					// System.out.println("left"+leftValue);
					// System.out.println("right"+rightValue);
					// System.out.println("result"+result);
					// System.out.println("threeValue"+threeValue);
					// System.out.println("preIsLeft"+preIsLeft);
					// System.out.println(" ");
					break;
				}
				// System.out.println("upperCount-lowerCount"+ (upperCount-lowerCount));
				result += Math.abs(leftValue-threeValue);
				leftValue = threeValue;
				lowerCount++;
				preIsLeft =true;
				break;

					
				default:
				// System.out.println("omg");
					break;
			}
		}
		System.out.println(result);


	}

}