Codeforces Round #642 (Div. 3) B. Two Arrays And Swaps

You are given two arrays aa and bb both consisting of nn positive (greater than zero) integers. You are also given an integer kk .

In one move, you can choose two indices ii and jj (1i,jn1≤i,j≤n ) and swap aiai and bjbj (i.e. aiai becomes bjbj and vice versa). Note that ii and jj can be equal or different (in particular, swap a2a2 with b2b2 or swap a3a3 and b9b9 both are acceptable moves).

Your task is to find the maximum possible sum you can obtain in the array aa if you can do no more than (i.e. at most) kk such moves (swaps).

You have to answer tt independent test cases.

Input

The first line of the input contains one integer tt (1t2001≤t≤200 ) — the number of test cases. Then tt test cases follow.

The first line of the test case contains two integers nn and kk (1n30;0kn1≤n≤30;0≤k≤n ) — the number of elements in aa and bb and the maximum number of moves you can do. The second line of the test case contains nn integers a1,a2,,ana1,a2,…,an (1ai301≤ai≤30 ), where aiai is the ii -th element of aa . The third line of the test case contains nn integers b1,b2,,bnb1,b2,…,bn (1bi301≤bi≤30 ), where bibi is the ii -th element of bb .

Output

For each test case, print the answer — the maximum possible sum you can obtain in the array aa if you can do no more than (i.e. at most) kk swaps.

Example
Input
Copy
5
2 1
1 2
3 4
5 5
5 5 6 6 5
1 2 5 4 3
5 3
1 2 3 4 5
10 9 10 10 9
4 0
2 2 4 3
2 4 2 3
4 4
1 2 2 1
4 4 5 4
Output
Copy
6
27
39
11
17
sort后分别取前k个,后k个进行比较,如果能使答案变优就交换。
复制代码
#include <bits/stdc++.h>
using namespace std;
int a[35],b[35],n,k;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>k;
        int i,ans=0;
        for(i=1;i<=n;i++) scanf("%d",&a[i]);
        for(i=1;i<=n;i++) scanf("%d",&b[i]);
        sort(a+1,a+n+1);
        sort(b+1,b+n+1);
        for(i=1;i<=k;i++)
        {
            if(a[i]>b[n-i+1]) break;
            else swap(a[i],b[n-i+1]);
        }
        for(i=1;i<=n;i++)ans+=a[i];
        cout<<ans<<endl;
    }
    return 0;
}
复制代码

 

posted @   脂环  阅读(481)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示
主题色彩