|
| 1 | +/* |
| 2 | +You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k. |
| 3 | +
|
| 4 | +In one move, you can choose two indices i and j (1=i,j=n) and swap ai and bj (i.e. ai becomes bj and vice versa). Note that i and j can be equal or different (in particular, swap a2 with b2 or swap a3 and b9 both are acceptable moves). |
| 5 | +
|
| 6 | +Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k such moves (swaps). |
| 7 | +
|
| 8 | +You have to answer t independent test cases. |
| 9 | +
|
| 10 | +Input |
| 11 | +The first line of the input contains one integer t (1=t=200) — the number of test cases. Then t test cases follow. |
| 12 | +
|
| 13 | +The first line of the test case contains two integers n and k (1=n=30;0=k=n) — the number of elements in a and b and the maximum number of moves you can do. The second line of the test case contains n integers a1,a2,…,an (1=ai=30), where ai is the i-th element of a. The third line of the test case contains n integers b1,b2,…,bn (1=bi=30), where bi is the i-th element of b. |
| 14 | +
|
| 15 | +Output |
| 16 | +For each test case, print the answer — the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps. |
| 17 | +
|
| 18 | +Example |
| 19 | +inputCopy |
| 20 | +5 |
| 21 | +2 1 |
| 22 | +1 2 |
| 23 | +3 4 |
| 24 | +5 5 |
| 25 | +5 5 6 6 5 |
| 26 | +1 2 5 4 3 |
| 27 | +5 3 |
| 28 | +1 2 3 4 5 |
| 29 | +10 9 10 10 9 |
| 30 | +4 0 |
| 31 | +2 2 4 3 |
| 32 | +2 4 2 3 |
| 33 | +4 4 |
| 34 | +1 2 2 1 |
| 35 | +4 4 5 4 |
| 36 | +outputCopy |
| 37 | +6 |
| 38 | +27 |
| 39 | +39 |
| 40 | +11 |
| 41 | +17 |
| 42 | +Note |
| 43 | +In the first test case of the example, you can swap a1=1 and b2=4, so a=[4,2] and b=[3,1]. |
| 44 | +
|
| 45 | +In the second test case of the example, you don't need to swap anything. |
| 46 | +
|
| 47 | +In the third test case of the example, you can swap a1=1 and b1=10, a3=3 and b3=10 and a2=2 and b4=10, so a=[10,10,10,4,5] and b=[1,9,3,2,9]. |
| 48 | +
|
| 49 | +In the fourth test case of the example, you cannot swap anything. |
| 50 | +
|
| 51 | +In the fifth test case of the example, you can swap arrays a and b, so a=[4,4,5,4] and b=[1,2,2,1]. |
| 52 | +*/ |
| 53 | + |
| 54 | + |
| 55 | +#include<bits/stdc++.h> |
| 56 | +using namespace std; |
| 57 | + |
| 58 | +int main(){ |
| 59 | + ios_base::sync_with_stdio(false); |
| 60 | + cin.tie(NULL); |
| 61 | + |
| 62 | + int t; |
| 63 | + cin >> t; |
| 64 | + |
| 65 | + while (t--) { |
| 66 | + int n, k; |
| 67 | + cin >> n >> k; |
| 68 | + |
| 69 | + vector <int> a(n), b(n); |
| 70 | + |
| 71 | + for (int i = 0; i < n; i++) cin >> a[i]; |
| 72 | + for (int i = 0; i < n; i++) cin >> b[i]; |
| 73 | + |
| 74 | + sort(a.begin(), a.end()); |
| 75 | + sort(b.begin(), b.end(), greater<int>()); |
| 76 | + |
| 77 | + int i = 0; |
| 78 | + while (k--) { |
| 79 | + if (a[i] > b[i]) break; |
| 80 | + swap(a[i], b[i]); |
| 81 | + i++; |
| 82 | + } |
| 83 | + |
| 84 | + int sum = 0; |
| 85 | + |
| 86 | + for (auto &x: a) sum += x; |
| 87 | + |
| 88 | + cout << sum << endl; |
| 89 | + } |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
| 93 | + |
0 commit comments