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)

#![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::cmp::Reverse;
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;
    }

    // 貪欲に作っていこう
    let mut rest_q: VecDeque<i64> = As.clone().into_iter().collect();
    let mut ans_q = VecDeque::new();

    let a0 = rest_q.pop_front().unwrap();
    ans_q.push_back(a0);

    while !rest_q.is_empty() {
        let rest_fr = *rest_q.front().unwrap();
        let rest_bc = *rest_q.back().unwrap();

        let ans_fr = *ans_q.front().unwrap();
        let ans_bc = *ans_q.back().unwrap();

        let mut cands = vec![
            (ans_fr - rest_fr).abs(),
            (ans_fr - rest_bc).abs(),
            (ans_bc - rest_fr).abs(),
            (ans_bc - rest_bc).abs(),
        ];
        cands.sort_by_key(|&num| Reverse(num));
        let maxx = cands[0];

        if (ans_fr - rest_fr).abs() == maxx {
            rest_q.pop_front();
            ans_q.push_front(rest_fr);
        } else if (ans_fr - rest_bc).abs() == maxx {
            rest_q.pop_back();
            ans_q.push_front(rest_bc);
        } else if (ans_bc - rest_fr).abs() == maxx {
            rest_q.pop_front();
            ans_q.push_back(rest_fr);
        } else if (ans_bc - rest_bc).abs() == maxx {
            rest_q.pop_back();
            ans_q.push_back(rest_bc);
        }
    }

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

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

    println!("{}", ans);

    // survey(N, &As);
}