macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } #[allow(unused_imports)] use std::ops::*; #[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; fn main() { input! { n: usize, m: usize, w: i64, al: [i64; n], bl: [i64; n], cl: [i64; m], dl: [i64; m], } let mut ans = 0; for i in 0..(1 << n) { let mut wvl = vec![]; for j in 0..n { if (1 << j) & i != 0 { wvl.push((al[j], bl[j])); } } wvl.sort_by_key(|e| -e.0); for s in 0..(1 << m) { let mut cdl = vec![]; for t in 0..m { if (1 << t) & s != 0 { cdl.push((cl[t], dl[t])); } } cdl.sort_by_key(|e| e.0); let mut cw = 0; let mut cv = 0; let mut ok = true; for (x, y) in wvl.iter() { if cw + *x >= w { for i in 0..cdl.len() { if cw >= cdl[i].0 { cw -= cdl[i].0; cv -= cdl[i].1; } } } cw += *x; cv += *y; if cw > w || cw < 0 { ok = false; break; } } if ok { ans = max(ans, cv); } } } println!("{}", ans); }