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 Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(non_snake_case)]
#![allow(unused_imports)]
use ascii::AsciiChar;
use ascii::AsciiStr;
use itertools::Itertools as _;
use permutohedron::LexicalPermutation as _;
use petgraph::algo::dijkstra;
use petgraph::algo::min_spanning_tree;
use petgraph::data::FromElements;
use petgraph::graph::NodeIndex;
use petgraph::graph::UnGraph;
use petgraph::unionfind::UnionFind;
use proconio::fastout;
use proconio::input;
use proconio::marker::Chars;
use proconio::marker::Usize1;
use std::cmp::max;
use std::cmp::min;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
// use superslice::Ext as _;

#[allow(dead_code)]
fn survey(N: usize, v: &Vec<i64>) -> i64 {
    let mut As: Vec<i64> = v.clone();

    As.sort();

    let mut data = As.clone();
    let mut permutations = Vec::new();

    loop {
        permutations.push(data.to_vec());
        if !data.next_permutation() {
            break;
        }
    }

    let mut ans: i64 = 0;
    for permutation in permutations.iter() {
        let mut cand: i64 = 0;
        for i in 1..N {
            cand += (permutation[i] - permutation[i - 1]).abs();
        }

        const ANSWER: i64 = 25;
        if cand == ANSWER {
            dbg!(permutation, cand);
        }
        ans = max(ans, cand);
    }

    return ans;
}

#[fastout]
fn main() {
    input! {
        N: usize,
        mut As: [i64; N],
    }

    As.sort();

    if N == 2 {
        let ans = (As[1] - As[0]).abs();
        println!("{}", ans);
        return;
    } else if N == 3 {
        let ans = (As[1] - As[0]).abs() + (As[2] - As[0]).abs();
        println!("{}", ans);
        return;
    }

    // 貪欲に作っていこう

    let mut q = VecDeque::new();
    q.push_back(As[0]);
    q.push_back(As[N - 1]);
    q.push_front(As[N - 2]);

    // 残り、[1, N-2) を埋めていく
    // (N - 3) / 2 ペア
    let pair_cnt = (N - 3) / 2;
    for i in 1..1 + pair_cnt {
        let a = As[i];
        let b = As[N - 2 - i];

        let fr0 = q.front().unwrap();
        let bc0 = q.back().unwrap();
        if (fr0 - a).abs() > (bc0 - a).abs() {
            q.push_front(a);
        } else {
            q.push_back(a);
        }

        let fr1 = q.front().unwrap();
        let bc1 = q.back().unwrap();
        if (fr1 - b).abs() > (bc1 - b).abs() {
            q.push_front(b);
        } else {
            q.push_back(b);
        }
    }

    // 残りの1個 (As[1+pair_cnt]) があればそれを最適なほうに付けて終わり
    if N % 2 == 0 {
        let a = As[1 + pair_cnt];
        let fr = q.front().unwrap();
        let bc = q.back().unwrap();

        if (fr - a).abs() > (bc - a).abs() {
            q.push_front(a);
        } else {
            q.push_back(a);
        }
    }

    let mut ans: i64 = 0;
    let v: Vec<i64> = q.into_iter().collect();

    for i in 1..N {
        ans += (v[i] - v[i - 1]).abs();
    }

    println!("{}", ans);

    // survey(N, &As);
}