fn run(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter) { let t: u32 = sc.next(); for _ in 0..t { let n: usize = sc.next(); let k: usize = sc.next(); let a: Vec = sc.next_vec(n); let b: Vec = sc.next_vec(n); let ans = solve(a, b, k); writeln!(out, "{}", ans).ok(); } } fn solve(a: Vec, b: Vec, k: usize) -> i64 { let inf = std::i64::MAX / 2; let eval = |m: i64| -> (i64, usize) { let mut dp = [(-inf, 0); 2]; dp[0] = (0, 0); for (&a, &b) in a.iter().zip(b.iter()) { let mut next = [(-inf, 0); 2]; for (i, dp) in dp.iter().enumerate() { next[i].chmax(*dp); next[i ^ 1].chmax((dp.0 - m, dp.1 + 1)); } next[0].0 += a; next[1].0 += b; dp = next; } dp[0].max(dp[1]) }; if eval(0).1 <= k { return eval(0).0; } let mut l = 0; let mut r = inf - 1; while r - l > 1 { let m = (l + r) / 2; if eval(m).1 > k { l = m; } else { r = m; } } eval(r).0 + k as i64 * r } // ---------- begin scannner ---------- #[allow(dead_code)] mod scanner { use std::str::FromStr; pub struct Scanner<'a> { it: std::str::SplitWhitespace<'a>, } impl<'a> Scanner<'a> { pub fn new(s: &'a String) -> Scanner<'a> { Scanner { it: s.split_whitespace(), } } pub fn next(&mut self) -> T { self.it.next().unwrap().parse::().ok().unwrap() } pub fn next_bytes(&mut self) -> Vec { self.it.next().unwrap().bytes().collect() } pub fn next_chars(&mut self) -> Vec { self.it.next().unwrap().chars().collect() } pub fn next_vec(&mut self, len: usize) -> Vec { (0..len).map(|_| self.next()).collect() } } } // ---------- end scannner ---------- use std::io::Write; use std::collections::*; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut sc = scanner::Scanner::new(&s); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); run(&mut sc, &mut out); } // ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ----------