#652 (Div. 2)C. RationalLee(贪心)

题目描述

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 n integers a1,a2,…,an in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly wi 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 t (1≤t≤104) — the number of test cases.
Next 3t lines contain test cases — one per three lines.
The first line of each test case contains two integers n and k (1≤n≤2⋅105; 1≤k≤n) — the number of integers Lee has and the number of Lee’s friends.
The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) — the integers Lee has.
The third line contains k integers w1,w2,…,wk (1≤wi≤n; w1+w2+…+wk=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 2⋅105.

Output

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

Example

input
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
48
42
8000000000

Note

In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17+17) and remaining integers to the second friend (his happiness will be 13+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)
In the third test case, Lee has four friends and four integers, it doesn’t matter how he distributes the integers between his friends.

题目大意

给你n个数,有m个朋友,每个朋友要拿其中w[i]个数,每个人获得的贡献是他拿的数的最大值加最小值,如果只有一个数最大值最小值都是它,让你求出所有人能获得的最大贡献是多少?

题目分析

因为贡献值是由最大值加最小值得到的,那么就说明:最大值和最小值中间的数是没有用的。那么我们要做的就是让中间的这些数尽可能地少。
为了让中间的数尽可能地小,我们应该把a[]和w[]进行排序,把a[]中最小的数给w[]中最大的。
当w[i]=1时,最大值就等于最小值,我们应该把最大的数都给w[i]=1的情况。
此外,为了让答案最大,应该把a[]中前k大的数,分给每个w[],使得a[]中最大的数都能够被利用起来。
这样分配即可取得最大值。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
int a[N],w[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,k;
		scanf("%d%d",&n,&k);
		for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
		
		for(int i=0;i<k;i++)
		scanf("%d",&w[i]);
		
		sort(a,a+n);	//a从小到大排序
		sort(w,w+k,greater<int>());	//w从大到小进行排序
		LL ans=0;
		for(int i=0,j=n-1;i<k;i++,j--)
		{
			ans+=a[j];	//将前k大的数分配给每个人一个
		}
		int s=n-1;
		for(int i=0,j=0;i<k;i++)	//将a[]中小的分配给w[]中大的
		{	//w[i]==1时,要特殊处理
			if(w[i]==1) {ans+=a[s]; s--; continue;}
			ans+=a[j];	//加上这个w[i]的最小值
			j+=w[i]-1;	//因为中间的数没有贡献,所以可以直接略过
		}
		cout<<ans<<endl;
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章