use std::io::Read; fn run() { let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut it = s.trim().split_whitespace(); let n: usize = it.next().unwrap().parse().unwrap(); let mut a: Vec = it.map(|s| s.parse().unwrap()).collect(); a.sort(); let mut sum = a.clone(); sum.push(0); for i in (0..n).rev() { sum[i] += sum[i + 1]; } let mut ans = 0; for mid in 1..n { let f = |x: usize| { let len = mid - x; sum[n - len] + sum[x] - sum[mid + 1] - (2 * len + 1) as i64 * a[mid] }; let mut l = 0; let mut r = mid; while r - l > 10 { let ll = (2 * l + r) / 3; let rr = (l + 2 * r) / 3; if f(ll) <= f(rr) { l = ll; } else { r = rr; } } for i in l..r { ans = std::cmp::max(ans, f(i)); } } println!("{}", ans); } fn main() { run(); }