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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

func asub(x, y int) int {
	if x > y {
		return x - y
	}
	return y - x
}

func main() {
	sc.Split(bufio.ScanWords)

	N := getInt()
	a := make([]int, N)
	for i := 0; i < N; i++ {
		a[i] = getInt()
	}
	sort.Ints(a)
	lpos := 1
	rpos := len(a) - 1
	lval := 1
	rval := 1
	ans := 0
	for i := 1; i < N; i++ {
		l := a[lpos]
		r := a[rpos]
		d := [4]int{
			asub(lval, l),
			asub(lval, r),
			asub(rval, r),
			asub(rval, l),
		}
		sel := 0
		for i, v := range d {
			if d[sel] < v {
				sel = i
			}
		}
		//		out("v", lval, rval, " ", l, r, d, sel)
		switch sel {
		case 0: // ll
			lpos++
			lval = l
		case 1: // lr
			rpos--
			lval = r
		case 2: // rr
			rpos--
			rval = r
		case 3: // rl
			lpos++
			rval = l
		}
		ans += d[sel]
	}
	out(ans)
}