Good Bye 2022: 2023 is NEAR——A. Koxia and Whiteboards

136 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第34天,点击查看活动详情

Good Bye 2022: 2023 is NEAR——A. Koxia and Whiteboards

Problem - A - Codeforces

Kiyora has nn whiteboards numbered from 11 to nn. Initially, the ii-th whiteboard has the integer aiai written on it.

Koxia performs mm operations. The jj-th operation is to choose one of the whiteboards and change the integer written on it to bjbj.

Find the maximum possible sum of integers written on the whiteboards after performing all mm operations.

Input

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤1001≤n,m≤100).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

The third line of each test case contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤1091≤bi≤109).

Output

For each test case, output a single integer — the maximum possible sum of integers written on whiteboards after performing all mm operations.

Example

input

4
3 2
1 2 3
4 5
2 3
1 2
3 4 5
1 1
100
1
5 3
1 1 1 1 1
1000000000 1000000000 1000000000

output

12
9
1
3000000002

问题解析

题目是说给你一个长度为n的数组,你要对这个数组进行m次操作,每次操作给你一个数替换掉数组中的某一个数,问最后能得到的数组和最大是多少。

贪心做法。对数组和有影响的情况无非2种:

  • 当前替换的数比数组中的最小值要大,那么我们选最小值来进行替换,显然就可以提升更多的数组和。
  • 当前替换的数比数组中的最小值要小,那么我们选最小值来进行替换,显然损失的数组和就会更少。

所以,我们每次选择数组中最小的那个数进行操作即可。这一点可以用优先队列实现。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 998244353;
​
int a[N];
void solve()
{
    int n, m, x;
    cin >> n >> m;
    priority_queue<int, vector<int>, greater<int>>que;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        que.push(x);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> x;
        que.pop();
        que.push(x);
    }
    int sum = 0;
    
    for (int i = 0; i < n; i++)
    {
        sum += que.top();
        que.pop();
    }
    cout << sum << endl;
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
​