CF1369C - From Codeforces Round #652 (Div. 2)

Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it’s time to distribute them between his friends rationally…
Lee has nn integers a1,a2,,ana_1,a_2,…,a_n in his backpack and he has kk friends. Lee would like to distribute all integers in his backpack between his friends, such that the ii-th friend will get exactly wiw_i integers and each integer will be handed over to exactly one friend.
Let’s define the happiness of a friend as the sum of the maximum and the minimum integer he’ll get.
Lee would like to make his friends as happy as possible, in other words, he’d like to maximize the sum of friends’ happiness. Now he asks you to calculate the maximum sum of friends’ happiness.

Input

The first line contains one integer tt (1t104)(1≤t≤10^4) — the number of test cases.
Next 3t3t lines contain test cases — one per three lines.
The first line of each test case contains two integers nn and kk (1n21051≤n≤2⋅10^5; 1kn1≤k≤n) — the number of integers Lee has and the number of Lee’s friends.
The second line of each test case contains nn integers a1,a2,,ana_1,a_2,…,a_n (109ai109−10^9≤a_i≤10^9) — the integers Lee has.
The third line contains kk integers w1,w2,,wkw_1,w_2,…,w_k (1win1≤w_i≤n; w1+w2++wk=nw_1+w_2+…+w_k=n) — the number of integers Lee wants to give to each friend.
It’s guaranteed that the sum of n over test cases is less than or equal to 21052⋅10^5.

Output

For each test case, print a single integer — the maximum sum of happiness Lee can achieve.

Example

input

1
2
3
4
5
6
7
8
9
10
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1

output

1
2
3
48
42
8000000000

Note

In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17+1717+17) and remaining integers to the second friend (his happiness will be 13+113+1).
In the second test case, Lee should give $\{10,10,11\}$ to the first friend and to the second friend, so the total happiness will be equal to (11+10)+(11+10)(11+10)+(11+10).
In the third test case, Lee has four friends and four integers, it doesn’t matter how he distributes the integers between his friends.

Translation

nn 个整数 a1,a2,,ana_1,a_2,\cdots,a_n,Lee 想要将它们放进 kk 个有标号的盒子,使得标号为 ii 的盒子中恰有 wiw_i 个数。
定义一个盒子的价值为其中最大数与最小数之和,定义一种放法的总价值为所有盒子的价值之和。Lee 希望求出总价值的最大值。

Idea

显然要让 kk 个最大的 aia_i 发挥作用,让 kk 个最小的 aia_i 尽可能少量的产生贡献。

对于 wi=1w_i=1 的袋子,将最大的 aia_i 放入,产生的贡献为 2ai2a_i,较大值直接产生两倍贡献,这肯定是最优的;对于 wi>1w_i>1 的袋子,将最大的 ai(max)a_{i(\max)} 放入,然后将最小的 ai(min)a_{i(\min)} 放入,中间依次填充较小的 aia_i,这样就能尽可能屏蔽 kk 个最小的 aia_i 产生的贡献。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/******************************************************************
Copyright: 11D_Beyonder All Rights Reserved
Author: 11D_Beyonder
Problem ID: CF1369C
Date: 01/14/2020
Description: Greedy, Math
*******************************************************************/
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=200006;
int _;
ll a[N],w[N];
int n,k;
int main(){
for(cin>>_;_;_--){
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=k;i++){
cin>>w[i];
}
ll ans=0;
int l=1,r=n;
//从大到小排
sort(a+1,a+1+n,greater<ll>());
sort(w+1,w+1+k,greater<ll>());
for(int i=k;i>=1;i--){
if(w[i]==1){
//先将最大的几个放入w=1中
//贡献为2倍
ans+=2*a[l++];
}else{
break;
}
}
for(int i=1;i<=k;i++){
if(w[i]==1){
break;
}else{
//将最大的先放入 再放入最小的
//中间w-2个用小的填补 相当于小的贡献被忽略了
ans+=a[l++]+a[r--];
r-=w[i]-2;
}
}
cout<<ans<<endl;
}
return 0;
}