#[allow(unused_imports)] use std::{ convert::{Infallible, TryFrom, TryInto as _}, fmt::{self, Debug, Display, Formatter,}, fs::File, hash::{Hash, Hasher, BuildHasherDefault}, iter::{Product, Sum}, marker::PhantomData, ops::{Add, AddAssign, Sub, SubAssign, Div, DivAssign, Mul, MulAssign, Neg, RangeBounds}, str::FromStr, sync::{atomic::{self, AtomicU32, AtomicU64}, Once}, collections::{*, btree_set::Range, btree_map::Range as BTreeRange}, mem::{swap}, cmp::{self, Reverse, Ordering, Eq, PartialEq, PartialOrd}, thread::LocalKey, f64::consts::PI, time::Instant, cell::RefCell, io::{self, stdin, Read, read_to_string, BufWriter, BufReader, stdout, Write}, }; pub trait SortD{ fn sort_d(&mut self); } impl SortD for Vec{ fn sort_d(&mut self) { self.sort_by(|u, v| v.cmp(&u)); } } pub trait Mx{fn max(&self, rhs: Self)->Self;} impl Mx for f64{ fn max(&self, rhs: Self)->Self{if *self < rhs{ rhs } else { *self } }} pub trait Mi{ fn min(&self, rhs: Self)->Self; } impl Mi for f64{ fn min(&self, rhs: Self)->Self{ if *self > rhs{ rhs } else { *self } } } pub trait Chmax: PartialOrd + Copy { fn chmax(&mut self, rhs: Self) { if *self < rhs { *self = rhs; } } } impl Chmax for T {} pub trait Chmin: PartialOrd + Copy { fn chmin(&mut self, rhs: Self) { if *self > rhs { *self = rhs; } } } impl Chmin for T {} #[allow(unused_imports)] use proconio::{input, input_interactive, marker::{*}, fastout}; /* #[allow(unused_imports)] use rustc_hash::FxHasher; #[allow(dead_code)] type FxMap = HashMap>; #[allow(dead_code)] type FxSet = HashSet>; #[allow(unused_imports)] use rand::{rng, Rng, seq::SliceRandom, prelude::*, *}; #[allow(unused_imports)] use itertools::Itertools; #[allow(unused_imports)] use ordered_float::OrderedFloat; #[allow(unused_imports)] use num_bigint::BigInt; #[allow(unused_imports)] use ac_library::{*, modint::ModInt998244353 as Mint};type MI = StaticModInt;pub fn factorial_mint(n: usize)->(Vec, Vec){ let mut res = vec![MI::new(1); n+1];let mut inv = vec![MI::new(1); n+1];for i in 0..n{res[i+1] = res[i]*(i+1);}inv[n] = MI::new(1)/res[n];for i in (0..n).rev(){inv[i] = inv[i+1]*(i+1);}(res, inv)}pub fn cm(a: usize, b: usize, mf: &(Vec, Vec))->MI{if a(usize, usize){match c{b'U'=>(!0,0),b'D'=>(1,0),b'L'=>(0,!0),b'R'=>(0,1),_=>unreachable!()}} #[allow(dead_code)] pub fn c2d_i64(c: u8)->(i64, i64){match c{b'U'=>(-1,0),b'D'=>(1,0),b'L'=>(0,-1),b'R'=>(0,1),_=>unreachable!()}} #[allow(dead_code)] const D2: [(usize, usize); 8] = [(1, 0), (1, 1), (0, 1), (!0, 1), (!0, 0), (!0, !0), (0, !0), (1, !0)]; const MULTI: bool = false; #[fastout] fn solve(){ input!{ n: usize, r: [i64; n], } if n > 25{ println!("0"); return; } let h1 = &r[..n/2]; let h2 = &r[n/2..]; let mut ans = 1<<60; let mut set1 = HashMap::new(); let mut set2 = HashMap::new(); let mut set3 = HashMap::new(); for i in 0..3i64.pow(h1.len() as u32){ let mut cnt = 0; let mut z = 0; let mut x = i; let (mut f1, mut f2) = (false, false); for j in 0..h1.len(){ if x%3==0{ f1 = true; z += h1[j]; cnt += 1; } else if x%3==1{ f2 = true; z -= h1[j]; cnt -= 1; } x /= 3; } if f1 && f2{ set1.entry(cnt).or_insert(BTreeSet::new()).insert(z); } else if f1{ set2.entry(cnt).or_insert(BTreeSet::new()).insert(z); } else if f2{ set3.entry(cnt).or_insert(BTreeSet::new()).insert(z); } } for i in 0..3i64.pow(h2.len() as u32){ let mut z = 0; let mut x = i; let mut cnt = 0; let (mut f1, mut f2) = (false, false); for j in 0..h2.len(){ if x%3==0{ f1 = true; z += h2[j]; cnt -= 1; } else if x%3==1{ f2 = true; z -= h2[j]; cnt += 1; } x /= 3; } if let Some(v) = set1.get(&cnt){ if let Some(&p) = v.range(-z..).next(){ ans = ans.min((p+z).abs()); } if let Some(&p) = v.range(..-z).next_back(){ ans = ans.min((p+z).abs()); } } if f1{ if let Some(v) = set3.get(&cnt){ if let Some(&p) = v.range(-z..).next(){ ans = ans.min((p+z).abs()); } if let Some(&p) = v.range(..-z).next_back(){ ans = ans.min((p+z).abs()); } } } if f2{ if let Some(v) = set2.get(&cnt){ if let Some(&p) = v.range(-z..).next(){ ans = ans.min((p+z).abs()); } if let Some(&p) = v.range(..-z).next_back(){ ans = ans.min((p+z).abs()); } } } } println!("{}", ans); } //#[fastout] fn main() { if MULTI{ input!{ t: usize, } for _ in 0..t{ solve(); } } else { solve(); } }