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 sw = 0i64; let mut sv = 0i64; for j in 0..n { if (1 << j) & i != 0 { sw += al[j]; sv += bl[j]; } } for s in 0..(1 << m) { let mut cw = sw; let mut cv = sv; for t in 0..m { if (1 << t) & s != 0 { cw -= cl[t]; cv -= dl[t]; } } if cw <= w { ans = max(ans, cv); } } } println!("{}", ans); }