The adversary can force you to draw all of the larger supplies of left or right gloves for every color before you manage to form a single matching pair, so you first need to draw the total obtained by adding, for each color, whichever is larger between its left glove count and its right glove count. At that point you might still have zero matches. Drawing one more glove guarantees that you form your first matching pair. To collect k matching pairs of different colors, after securing the first pair you must likewise force the remaining k minus one colors; each such color requires drawing additional gloves equal to whichever is smaller between its left glove count and its right glove count. Therefore, the minimum number of gloves you must draw is the sum of all the larger counts, plus the sum of the k minus one largest smaller counts, plus one additional glove. solution :
include <bits/stdc++.h>
using namespace std; using ll = long long;
int main(){ ios::sync_with_stdio(false); cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
vector<ll> l(n), r(n);
for (int i = 0; i < n; i++) cin >> l[i];
for (int i = 0; i < n; i++) cin >> r[i];
vector<ll> m(n);
ll sum_max = 0;
for (int i = 0; i < n; i++) {
m[i] = min(l[i], r[i]);
sum_max += max(l[i], r[i]);
}
sort(m.begin(), m.end(), greater<ll>());
ll sum_m = 0;
for (int i = 0; i < k-1; i++) {
sum_m += m[i];
}
cout << (sum_max + sum_m + 1) << "\n";
}
return 0;}



