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.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStream;


public class Main {
    void run() {
        Kattio io = new Kattio(System.in, System.out);
        int n = io.getInt();
        if (n == 1) {
            System.out.println(0);
            return;
        }
        long res = 0;
        long[] array = new long[n];
        for (int i = 0; i < n; i++)
            array[i] = io.getLong();
        Arrays.sort(array);
        int l = 1, r = n - 2, front = n-1, tail = 0, cnt=1;
        res += Math.abs(array[front] - array[tail]);
        while (l <= r) {
            if (cnt % 2 == 1) {
                long a = Math.abs(array[l]-array[tail]);
                long b = Math.abs(array[l]-array[front]);
                if (a > b) {
                    res += a;
                    tail = l;
                } else {
                    res += b;
                    front = l;
                }
                l++;
                cnt++;
            } else {
                long a = Math.abs(array[r]-array[tail]);
                long b = Math.abs(array[r]-array[front]);
                if (a > b) {
                    res += a;
                    tail = r;
                } else {
                    res += b;
                    front = r;
                }
                cnt++;
                r--;
            }
        }
        System.out.println(res);
    }

    public static void main (String [] args) {
        new Main().run();
    }
}

class Kattio extends PrintWriter {
    public Kattio(InputStream i) {
		super(new BufferedOutputStream(System.out));
		r = new BufferedReader(new InputStreamReader(i));
    }
    public Kattio(InputStream i, OutputStream o) {
		super(new BufferedOutputStream(o));
		r = new BufferedReader(new InputStreamReader(i));
    }

    public boolean hasMoreTokens() {
		return peekToken() != null;
    }

    public int getInt() {
		return Integer.parseInt(nextToken());
    }

    public double getDouble() { 
		return Double.parseDouble(nextToken());
    }

    public long getLong() {
		return Long.parseLong(nextToken());
    }

    public String getWord() {
		return nextToken();
    }

    private BufferedReader r;
    private String line;
    private StringTokenizer st;
    private String token;

    private String peekToken() {
	if (token == null) 
	    try {
		      while (st == null || !st.hasMoreTokens()) {
		    	line = r.readLine();
		    	if (line == null) return null;
		    	st = new StringTokenizer(line);
			}
				token = st.nextToken();
	    	} catch (IOException e) { }
		return token;
    }

    private String nextToken() {
		String ans = peekToken();
		token = null;
		return ans;
    }
}