#include using namespace std; using ll=long long; #define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++) vector candy(vector v, int K){ int N = v.size(); vector L(N), R(N); rep(i, 0, N) L[i] = i - 1, R[i] = i + 1; auto del = [&](int ind) -> void { if (ind == -1 || ind == N) return; if (L[ind] == -1){ if (R[ind] != N) L[R[ind]] = -1; } else{ if (R[ind] == N) R[L[ind]] = N; else{ int l = L[ind]; int r = R[ind]; R[l] = r; L[r] = l; } } R[ind] = -1; }; vector ans = {0}; priority_queue> pq; rep(i, 0, N) pq.push({v[i], i}); rep(rp, 0, (N + 1) / 2){ if ((int)ans.size() > K) break; while (true){ auto [val, ind] = pq.top(); pq.pop(); if (R[ind] == -1 || v[ind] != val){ continue; } ans.push_back(ans.back() + val); int l = L[ind]; int r = R[ind]; del(ind); if (l == -1 || r == N){ del(l), del(r); } else{ del(r); v[l] = v[l] - v[ind] + v[r]; pq.push({v[l], l}); } break; } } return ans; } void solve(); // POP'N ROLL MUSIC / TOMOO int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; cin >> t; rep(i, 0, t) solve(); } void solve(){ int N, K; cin >> N >> K; vector A(N + 1), B(N + 1); rep(i, 0, N) cin >> A[i + 1]; rep(i, 0, N) cin >> B[i + 1]; const ll inf = (1ll << 50); B[0] = -inf; vector> p; N++; if (K % 2 == 0){ A.push_back(0); B.push_back(-inf); N++; K++; } B.push_back(0); A.push_back(-inf); N++; ll ans = 0; // 圧縮する rep(i, 0, N){ if (A[i] >= B[i]) p.push_back({0, A[i] - B[i]}); else p.push_back({1, B[i] - A[i]}); ans += max(A[i], B[i]); } vector C; for (int l = 0, r = 0; l < N; l = r){ while (r != N && p[l].first == p[r].first) r++; ll tmp = 0; rep(i, l, r) tmp += p[i].second; C.push_back(-tmp); } // S[i] != T[i] の index の数を K に置き換える K = max(0, (int)(C.size()) - 1 - K); K /= 2; // C に対して、JOI 飴を解き、ans に加算する ans += candy(C, K)[K]; // 答えの出力 cout << ans << "\n"; }