Petya organized a strange birthday party. He invited 𝑛 friends and assigned an integer 𝑘𝑖 to the 𝑖-th of them. Now Petya would like to give a present to each of them. In the nearby shop there are 𝑚 unique presents available, the 𝑗-th present costs 𝑐𝑗 dollars (1≤𝑐1≤𝑐2≤…≤𝑐𝑚). It’s not allowed to buy a single present more than once.
For the 𝑖-th friend Petya can either buy them a present 𝑗≤𝑘𝑖, which costs 𝑐𝑗 dollars, or just give them 𝑐𝑘𝑖 dollars directly.
Help Petya determine the minimum total cost of hosting his party.
Input
The first input line contains a single integer 𝑡 (1≤𝑡≤103) — the number of test cases.
The first line of each test case contains two integers 𝑛 and 𝑚 (1≤𝑛,𝑚≤3⋅105) — the number of friends, and the number of unique presents available.
The following line contains 𝑛 integers 𝑘1,𝑘2,…,𝑘𝑛 (1≤𝑘𝑖≤𝑚), assigned by Petya to his friends.
The next line contains 𝑚 integers 𝑐1,𝑐2,…,𝑐𝑚 (1≤𝑐1≤𝑐2≤…≤𝑐𝑚≤109) — the prices of the presents.
It is guaranteed that sum of values 𝑛 over all test cases does not exceed 3⋅105, and the sum of values 𝑚 over all test cases does not exceed 3⋅105.
Output
For each test case output a single integer — the minimum cost of the party.
Examples
inputCopy
2
5 4
2 3 4 3 2
3 5 12 20
5 5
5 4 3 2 1
10 40 90 160 250
outputCopy
30
190
inputCopy
1
1 1
1
1
outputCopy
1
Note
In the first example, there are two test cases. In the first one, Petya has 5 friends and 4 available presents. Petya can spend only 30 dollars if he gives
5 dollars to the first friend.
A present that costs 12 dollars to the second friend.
A present that costs 5 dollars to the third friend.
A present that costs 3 dollars to the fourth friend.
5 dollars to the fifth friend.
In the second one, Petya has 5 and 5 available presents. Petya can spend only 190 dollars if he gives
A present that costs 10 dollars to the first friend.
A present that costs 40 dollars to the second friend.
90 dollars to the third friend.
40 dollars to the fourth friend.
10 dollars to the fifth friend.
思路:
先将
k
k
k数组排序
一开始每个人就选择
c
[
k
[
i
]
]
c[k[i]]
c[k[i]]代表直接给现金,然后再从小到大遍历礼物,从大到小遍历人,依次分配礼物。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + 7;
const int base = 1e9 + 7;
int k[maxn],c[maxn];
int main() {
int T;scanf("%d",&T);
while(T--) {
int n,m;scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++) {
scanf("%d",&k[i]);
}
for(int i = 1;i <= m;i++) {
scanf("%d",&c[i]);
}
ll ans = 0;
for(int i = 1;i <= n;i++) {
ans += c[k[i]];
}
sort(k + 1,k + 1 + n);
int cnt = 1; //第一个可选的位置
for(int i = n;i >= 1;i--) {
if(k[i] >= cnt) {
ans -= c[k[i]];
ans += c[cnt];
cnt++;
}
}
printf("%lld\n",ans);
}
return 0;
}