// 1-indexedなBIT // 座標xへの加算、[1,x]の和, ([1,x]の和)>=s となる最小のxの探索 // ---------- begin fenwick tree ---------- #[allow(dead_code)] mod fenwick { type T = i32; const ZERO: T = 0; pub struct Tree { a: Vec, } impl Tree { pub fn new(n: usize) -> Tree { Tree { a: vec![ZERO; n + 1], } } pub fn add(&mut self, mut x: usize, v: T) { while x < self.a.len() { self.a[x] += v; x += x & (!x + 1); } } pub fn sum(&self, mut x: usize) -> T { debug_assert!(x < self.a.len()); let mut res = ZERO; while x > 0 { res += self.a[x]; x -= x & (!x + 1); } res } pub fn search(&self, s: T) -> usize { debug_assert!(self.sum(self.a.len() - 1) >= s); let mut k = 1; while 2 * k < self.a.len() { k *= 2; } let mut x = 0; let mut w = ZERO; while k > 0 { if k + x < self.a.len() && w + self.a[x + k] < s { w += self.a[x + k]; x += k; } k >>= 1; } x + 1 } } } // ---------- end fenwick tree ---------- 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 a: Vec = (0..n).map(|_| it.next().unwrap().parse().unwrap()).collect(); let mut z = a.clone(); z.push(-1_000_000_000 - 1); z.sort(); z.dedup(); let mut index = vec![0; n]; for i in 0..n { index[i] = z.binary_search(&a[i]).unwrap(); } let mut ask = vec![]; for k in 1..=n { for i in 0..(n / k) { ask.push((i * k, (i + 1) * k)); ask.push((n - (i + 1) * k, n - i * k)); } } let b = (n as f64).sqrt() as usize; ask.sort_by_cached_key(|p| { let k = p.0 / b; if k & 1 == 0 { (k, p.1) } else { (k, n - p.1) } }); ask.dedup(); let mut bit = fenwick::Tree::new(z.len() - 1); let mut mid = std::collections::BTreeMap::<(usize, usize), i32>::new(); let mut x = 0; let mut y = 0; for (l, r) in ask { while y < r { bit.add(index[y], 1); y += 1; } while x > l { x -= 1; bit.add(index[x], 1); } while y > r { y -= 1; bit.add(index[y], -1); } while x < l { bit.add(index[x], -1); x += 1; } let k = bit.search(((r - l + 1) / 2) as i32) as usize; assert!(mid.insert((l, r), z[k]).is_none()); } let mut ans = 0i64; for k in 1..=n { let mut left = vec![]; let mut right = vec![]; for i in 0..(n / k) { left.push(*mid.get(&(i * k, (i + 1) * k)).unwrap() as i64); right.push(*mid.get(&(n - (i + 1) * k, n - i * k)).unwrap() as i64); } right.reverse(); left.push(0); right.push(0); for i in (0..(n / k)).rev() { left[i] += left[i + 1]; right[i] += right[i + 1]; } for i in (0..(n / k)).rev() { right[i] = std::cmp::max(right[i], right[i + 1]); } let mut local = 0; for i in 0..=(n / k) { let v = left[0] - left[i] + right[i]; local = std::cmp::max(local, v); } ans = std::cmp::max(ans, local * k as i64); } println!("{}", ans); } fn main() { run(); }