Submission #15895310


Source Code Expand

Copy
use proconio::input;

fn main() {
    input! {
        n: usize,
        mut a: [i64; n],
    };
    a.sort();
    let mut a = a.into_iter().collect::<std::collections::VecDeque<i64>>();
    let mut b = std::collections::VecDeque::<i64>::new();
    b.push_front(a.pop_front().unwrap());
    b.push_back(a.pop_back().unwrap());
    while !a.is_empty() {
        let a_f = *a.front().unwrap();
        let a_b = *a.back().unwrap();
        let b_f = *b.front().unwrap();
        let b_b = *b.back().unwrap();
        let v_afbf = (a_f - b_f).abs();
        let v_abbf = (a_b - b_f).abs();
        let v_afbb = (a_f - b_b).abs();
        let v_abbb = (a_b - b_b).abs();
        if v_afbf >= v_abbf && v_afbf >= v_afbb && v_afbf >= v_abbb {
            b.push_front(a.pop_front().unwrap());
        } else if v_abbf >= v_afbf && v_abbf >= v_afbb && v_abbf >= v_abbb {
            b.push_front(a.pop_back().unwrap());
        } else if v_afbb >= v_afbf && v_afbb >= v_abbf && v_afbb >= v_abbb {
            b.push_back(a.pop_front().unwrap());
        } else if v_abbb >= v_afbf && v_abbb >= v_abbf && v_abbb >= v_afbb {
            b.push_back(a.pop_back().unwrap());
        } else {
            unreachable!();
        }
    }

    let ans = b
        .iter()
        .enumerate()
        .skip(1)
        .map(|(i, &b_i)| (b_i - b[i - 1]).abs())
        .sum::<i64>();
    println!("{}", ans);
}

Submission Info

Submission Time
Task C - Align
User bouzuya
Language Rust (1.42.0)
Score 400
Code Size 1395 Byte
Status AC
Exec Time 33 ms
Memory 5240 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 40
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 32 ms 5104 KB
02.txt AC 31 ms 5152 KB
03.txt AC 28 ms 5240 KB
04.txt AC 26 ms 5196 KB
05.txt AC 32 ms 5116 KB
06.txt AC 33 ms 5204 KB
07.txt AC 31 ms 5168 KB
08.txt AC 32 ms 5200 KB
09.txt AC 25 ms 5240 KB
10.txt AC 23 ms 5056 KB
11.txt AC 33 ms 5188 KB
12.txt AC 30 ms 5100 KB
13.txt AC 33 ms 5168 KB
14.txt AC 30 ms 5196 KB
15.txt AC 28 ms 5192 KB
16.txt AC 28 ms 5216 KB
17.txt AC 32 ms 4816 KB
18.txt AC 30 ms 5132 KB
19.txt AC 25 ms 5216 KB
20.txt AC 30 ms 5196 KB
21.txt AC 30 ms 4912 KB
22.txt AC 25 ms 4648 KB
23.txt AC 30 ms 4880 KB
24.txt AC 23 ms 4644 KB
25.txt AC 22 ms 4492 KB
26.txt AC 22 ms 4492 KB
27.txt AC 22 ms 4408 KB
28.txt AC 17 ms 4380 KB
29.txt AC 9 ms 4484 KB
30.txt AC 17 ms 4948 KB
31.txt AC 15 ms 4876 KB
32.txt AC 22 ms 4852 KB
33.txt AC 1 ms 1984 KB
34.txt AC 2 ms 2024 KB
35.txt AC 2 ms 2076 KB
36.txt AC 2 ms 2020 KB
37.txt AC 1 ms 2040 KB
s1.txt AC 1 ms 2032 KB
s2.txt AC 2 ms 2044 KB
s3.txt AC 2 ms 2068 KB