use std::{i64::MAX, io::stdin}; fn main() { let stdin = stdin(); let mut stdin = stdin.lines().map(Result::unwrap); let n = stdin.next().unwrap().parse::().unwrap(); let mut a = stdin .next() .unwrap() .split_whitespace() .map(|x| x.parse::().unwrap()) .collect::>(); a.sort(); let mut dp = vec![MAX; n + 1]; dp[0] = 0; for i in 0..=n { if i == 1 { continue; } for k in 2..=3 { if i + k <= n { change_min!(&mut dp[i + k], dp[i] + a[i + k - 1] - a[i]); } } } let ans = dp[n]; println!("{}", ans); } // cmpmore {{{ #[allow(dead_code)] mod cmpmore { pub fn change_min(lhs: &mut T, rhs: T) { if *lhs > rhs { *lhs = rhs; } } pub fn change_max(lhs: &mut T, rhs: T) { if *lhs < rhs { *lhs = rhs; } } #[macro_export] macro_rules! change_min { ($lhs:expr, $rhs:expr) => { let rhs = $rhs; let lhs = $lhs; $crate::cmpmore::change_min(lhs, rhs); }; } #[macro_export] macro_rules! change_max { ($lhs:expr, $rhs:expr) => { let rhs = $rhs; let lhs = $lhs; $crate::cmpmore::change_max(lhs, rhs); }; } pub trait CmpMore: PartialOrd + Sized { fn change_min(&mut self, rhs: Self) { change_min(self, rhs) } fn change_max(&mut self, rhs: Self) { change_max(self, rhs) } } impl CmpMore for T {} } // }}}