#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章