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.15.1) (judge verdict: Accepted)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;
#[allow(unused_imports)]
use proconio::marker::*;
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::*;
#[allow(unused_imports)]
use std::f64::consts::*;

#[allow(unused)]
const INF: usize = std::usize::MAX / 4;
#[allow(unused)]
const M: usize = 1000000007;

fn main() {
    input! {
      n: usize,
      mut a: [usize; n],
    }
    a.sort();
    let mut q = VecDeque::new();
    for &ai in &a {
        q.push_back(ai);
    }
    let mut p = VecDeque::new();
    p.push_back(q.pop_front().unwrap());
    while !q.is_empty() {
        // println!("{:?} {:?}", p, q);
        let qf = q.front().unwrap();
        let qb = q.back().unwrap();
        let pf = p.front().unwrap();
        let pb = p.back().unwrap();
        let d1 = max(qf, pf) - min(qf, pf);
        let d2 = max(qf, pb) - min(qf, pb);
        let d3 = max(qb, pf) - min(qb, pf);
        let d4 = max(qb, pb) - min(qb, pb);
        if d1 >= d2 && d1 >= d3 && d1 >= d4 {
            let v = q.pop_front().unwrap();
            p.push_front(v);
        } else if d2 >= d1 && d2 >= d3 && d2 >= d4 {
            let v = q.pop_front().unwrap();
            p.push_back(v);
        } else if d3 >= d1 && d3 >= d2 && d3 >= d4 {
            let v = q.pop_back().unwrap();
            p.push_front(v);
        } else if d4 >= d1 && d4 >= d2 && d4 >= d3 {
            let v = q.pop_back().unwrap();
            p.push_back(v);
        }
    }
    let p = p.iter().collect::<Vec<_>>();
    // println!("{:?}", p);
    let mut s = 0;
    for i in 1..n {
        s += max(p[i], p[i - 1]) - min(p[i], p[i - 1])
    }
    println!("{}", s);
}
*/

fn main() {
    let exe = "/tmp/bin7C84290C";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";