CF1369C 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 integers in his backpack and he has friends. Lee would like to distribute all integers in his backpack between his friends, such that the -th friend will get exactly 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 — the number of test cases.
Next lines contain test cases — one per three lines.
The first line of each test case contains two integers and (; ) — the number of integers Lee has and the number of Lee’s friends.
The second line of each test case contains integers () — the integers Lee has.
The third line contains integers (; ) — 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 .
Output
For each test case, print a single integer — the maximum sum of happiness Lee can achieve.
Example
input
1 | 3 |
output
1 | 48 |
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be ) and remaining integers to the second friend (his happiness will be ).
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 .
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
有 个整数 ,Lee 想要将它们放进 个有标号的盒子,使得标号为 的盒子中恰有 个数。
定义一个盒子的价值为其中最大数与最小数之和,定义一种放法的总价值为所有盒子的价值之和。Lee 希望求出总价值的最大值。
Idea
显然要让 个最大的 发挥作用,让 个最小的 尽可能少量的产生贡献。
对于 的袋子,将最大的 放入,产生的贡献为 ,较大值直接产生两倍贡献,这肯定是最优的;对于 的袋子,将最大的 放入,然后将最小的 放入,中间依次填充较小的 ,这样就能尽可能屏蔽 个最小的 产生的贡献。
Code
1 | /****************************************************************** |