#![allow(unused_imports)] #![allow(non_snake_case, unused)] use std::cmp::*; use std::collections::*; use std::ops::*; macro_rules! eprint { ($($t:tt)*) => {{ use ::std::io::Write; let _ = write!(::std::io::stderr(), $($t)*); }}; } macro_rules! eprintln { () => { eprintln!(""); }; ($($t:tt)*) => {{ use ::std::io::Write; let _ = writeln!(::std::io::stderr(), $($t)*); }}; } macro_rules! dbg { ($v:expr) => {{ let val = $v; eprintln!("[{}:{}] {} = {:?}", file!(), line!(), stringify!($v), val); val }} } macro_rules! mat { ($($e:expr),*) => { Vec::from(vec![$($e),*]) }; ($($e:expr,)*) => { Vec::from(vec![$($e),*]) }; ($e:expr; $d:expr) => { Vec::from(vec![$e; $d]) }; ($e:expr; $d:expr $(; $ds:expr)+) => { Vec::from(vec![mat![$e $(; $ds)*]; $d]) }; } macro_rules! ok { ($a:ident$([$i:expr])*.$f:ident()$(@$t:ident)*) => { $a$([$i])*.$f($($t),*) }; ($a:ident$([$i:expr])*.$f:ident($e:expr$(,$es:expr)*)$(@$t:ident)*) => { { let t = $e; ok!($a$([$i])*.$f($($es),*)$(@$t)*@t) } }; } pub fn readln() -> String { let mut line = String::new(); ::std::io::stdin().read_line(&mut line).unwrap_or_else(|e| panic!("{}", e)); line } macro_rules! read { ($($t:tt),*; $n:expr) => {{ let stdin = ::std::io::stdin(); let ret = ::std::io::BufRead::lines(stdin.lock()).take($n).map(|line| { let line = line.unwrap(); let mut it = line.split_whitespace(); _read!(it; $($t),*) }).collect::>(); ret }}; ($($t:tt),*) => {{ let line = readln(); let mut it = line.split_whitespace(); _read!(it; $($t),*) }}; } macro_rules! _read { ($it:ident; [char]) => { _read!($it; String).chars().collect::>() }; ($it:ident; [u8]) => { Vec::from(_read!($it; String).into_bytes()) }; ($it:ident; usize1) => { $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::().unwrap_or_else(|e| panic!("{}", e)) - 1 }; ($it:ident; [usize1]) => { $it.map(|s| s.parse::().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::>() }; ($it:ident; [$t:ty]) => { $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::>() }; ($it:ident; $t:ty) => { $it.next().unwrap_or_else(|| panic!("input mismatch")).parse::<$t>().unwrap_or_else(|e| panic!("{}", e)) }; ($it:ident; $($t:tt),+) => { ($(_read!($it; $t)),*) }; } pub fn main() { let _ = ::std::thread::Builder::new().name("run".to_string()).stack_size(32 * 1024 * 1024).spawn(run).unwrap().join(); } const MOD: u32 = 1_000_000_007; const INF: i64 = std::i64::MAX; pub trait SetMinMax { fn setmin(&mut self, v: Self) -> bool; fn setmax(&mut self, v: Self) -> bool; } impl SetMinMax for T where T: PartialOrd { fn setmin(&mut self, v: T) -> bool { *self > v && { *self = v; true } } fn setmax(&mut self, v: T) -> bool { *self < v && { *self = v; true } } } #[derive(Clone)] struct BitSet { size: usize, a: Vec, } fn bit_size() -> usize { 8 * std::mem::size_of::() } fn quot_rem(n: usize) -> (usize, usize) { let w = bit_size(); (n / w, n % w) } #[allow(dead_code)] impl BitSet { fn new(size: usize) -> Self { let w = bit_size(); BitSet { size: size, a: vec![0; (size + w - 1) / w], } } fn set_at(&mut self, x: usize) { assert!(x < self.size); let (q, r) = quot_rem(x); self.a[q] |= 1 << r; } fn clear_at(&mut self, x: usize) { assert!(x < self.size); let (q, r) = quot_rem(x); self.a[q] &= !(1 << r); } fn get_at(&self, x: usize) -> bool { if x >= self.size { return false; } let (q, r) = quot_rem(x); (self.a[q] >> r) & 1 == 1 } fn any(&self) -> bool { self.a.iter().any(|a| *a != 0) } fn fix(&mut self) { let (q, r) = quot_rem(self.size); if r != 0 { self.a[q] &= (1 << r) - 1; } } fn clear(&mut self) { let len = self.a.len(); self.a.clear(); self.a.resize(len, 0); } fn truncate(&mut self, len: usize) { if len >= self.size { return; } let w = bit_size(); self.a.truncate((len + w - 1) / w); self.size = len; self.fix(); } fn popcnt(&self) -> usize { self.a.iter().fold(0, |s, a| s + a.count_ones() as usize) } fn shift_left(&self, rhs: usize) -> Self { let (q, r) = quot_rem(rhs); let mut ans = BitSet::new(self.size + rhs); if r == 0 { for (x, y) in ans.a[q..].iter_mut().zip(self.a.iter()) { *x = *y; } } else { let w = bit_size(); let mut prev = 0; for (x, y) in ans.a[q..].iter_mut().zip(self.a.iter()) { *x = (*y << r) | (prev >> (w - r)); prev = *y; } *ans.a.last_mut().unwrap() |= prev >> (w - r); } ans.fix(); ans } fn shift_right(&self, rhs: usize) -> Self { if rhs >= self.size { return BitSet::new(1); } let (q, r) = quot_rem(rhs); let mut ans = BitSet::new(self.size - rhs); if r == 0 { for (x, y) in ans.a.iter_mut().zip(self.a[q..].iter()) { *x = *y; } } else { let w = bit_size(); let mut prev = 0; for (x, y) in ans.a.iter_mut().zip(self.a[q..].iter()).rev() { *x |= (*y >> r) | (prev << (w - r)); prev = *y; } } ans.fix(); ans } fn bitwise_or(&self, rhs: &Self) -> Self { let (x, y) = if self.size >= rhs.size {(self, rhs)} else {(rhs, self)}; let mut a = x.a.clone(); for (a, y) in a.iter_mut().zip(y.a.iter()) { *a |= *y; } BitSet { size: x.size, a: a, } } fn bitwise_and(&self, rhs: &Self) -> Self { let (x, y) = if self.size <= rhs.size {(self, rhs)} else {(rhs, self)}; let mut a = x.a.clone(); for (a, y) in a.iter_mut().zip(y.a.iter()) { *a &= *y; } BitSet { size: x.size, a: a, } } fn bitwise_xor(&self, rhs: &Self) -> Self { let (x, y) = if self.size >= rhs.size {(self, rhs)} else {(rhs, self)}; let mut a = x.a.clone(); for (a, y) in a.iter_mut().zip(y.a.iter()) { *a ^= *y; } BitSet { size: x.size, a: a, } } fn bitwise_or_assign(&mut self, rhs: &Self) { if self.size < rhs.size { self.size = rhs.size; self.a.resize(rhs.a.len(), 0); } for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) { *a |= *b; } } fn bitwise_and_assign(&mut self, rhs: &Self) { if self.size > rhs.size { self.size = rhs.size; self.a.resize(rhs.a.len(), 0); } for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) { *a &= *b; } } fn bitwise_xor_assign(&mut self, rhs: &Self) { if self.size < rhs.size { self.size = rhs.size; self.a.resize(rhs.a.len(), 0); } for (a, b) in self.a.iter_mut().zip(rhs.a.iter()) { *a ^= *b; } } } fn solve() { let n = read!(usize); let x = read!([usize]; n); let a = read!([i64]); let mut ans = INF; let mut xb = vec![BitSet::new(n); n]; for i in 0..n { for j in 0..n { if x[i][j]==1 { xb[i].set_at(j); } } } for i in 0..(1<>j)&1==1 { sum += a[j]; } else{ c.set_at(j); } } if sum>=ans {continue;} let mut seen = vec![false;n]; let mut total = 0; for l in 0..n { for j in 0..n { if seen[j] {continue;} let tmp = xb[j].bitwise_and(&c); if !tmp.any() { seen[j]=true; c.clear_at(j); total += 1; } } } if total==n { ans = min(ans, sum); } } println!("{}", ans); } fn run() { solve(); }