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: Accepted)

use std::cmp;
use std::collections::VecDeque;
use std::fmt::Debug;
use std::str::FromStr;
#[allow(dead_code)]
fn read_as_vec<T>() -> Vec<T>
where
    T: FromStr,
    <T as FromStr>::Err: Debug,
{
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim()
        .split_whitespace()
        .map(|c| T::from_str(c).unwrap())
        .collect()
}

#[allow(dead_code)]
fn read_as_string() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main() {
    let n: usize = read_as_vec()[0];

    let mut a: Vec<i64> = vec![];
    let mut ans: i64 = 0;
    for _ in 0..n {
        a.push(read_as_vec()[0]);
    }
    a.sort();
    if a.len() % 2 == 0 {
        let mut a_smaller = a.iter().take(a.len() / 2).collect::<Vec<_>>();
        let mut a_larger = a.iter().skip(a_smaller.len()).collect::<Vec<_>>();
        a_smaller.sort();
        a_larger.sort();
        a_larger.reverse();
        ans = (a_larger.iter().map(|x| (**x) * 2).sum::<i64>() - *a_larger.last().unwrap())
            - (a_smaller.iter().map(|x| (**x) * 2).sum::<i64>() - *a_smaller.last().unwrap());
    } else {
        let mut a_smaller = a.iter().take(a.len() / 2).collect::<Vec<_>>();
        let mut a_larger = a.iter().skip(a_smaller.len()).collect::<Vec<_>>();
        a_smaller.sort();
        a_larger.sort();
        let pat1_ans =
            (a_larger.iter().map(|x| (**x) * 2).sum::<i64>() - a_larger[0] - a_larger[1])
                - a_smaller.iter().map(|x| (**x) * 2).sum::<i64>();

        // println!("smaller = {:?}", a_smaller);
        // println!("larger = {:?}", a_larger);

        let mut a_larger = a.iter().skip(a.len() / 2 + 1).collect::<Vec<_>>();
        let mut a_smaller = a.iter().take(a.len() - a_larger.len()).collect::<Vec<_>>();
        a_smaller.sort();
        a_smaller.reverse();
        a_larger.sort();
        let pat2_ans = a_larger.iter().map(|x| (**x) * 2).sum::<i64>()
            - (a_smaller.iter().map(|x| (**x) * 2).sum::<i64>() - a_smaller[0] - a_smaller[1]);
        //println!("pat1 = {}, pat2 = {}", pat1_ans, pat2_ans);
        // println!("smaller = {:?}", a_smaller);
        // println!("larger = {:?}", a_larger);
        ans = cmp::max(pat1_ans, pat2_ans);
    }
    println!("{}", ans);
}