Double Beauty

5

3 votes
Greedy Algorithms, Basics of Greedy Algorithms, Sorting, Algorithms, Merge Sort
Problem

You are given an array of length . You can select distinct indexes and double the element in them.

The beauty of the array is the sum of any subarray of length . Find the maximum beauty over all permutations of array .

Input Format:

  • The first line contains an integer , denoting the number of test cases.
  • The first line of each test case contains two integers and  respectively.
  • The second line of each test case contains space-separated integers, denoting the elements of the array.

Output Format:

For each test case, print the maximum beauty over all permutations of .

Constraints:

Sample Input
2
5 3
1 4 1 3 1
3 1
2 5 1
Sample Output
16
10
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

First test case:
We can select indexes , , and (1-based indexing).
becomes . The permutation that gives maximum beauty is . Hence, the answer is .

Second test case:
We can select index (1-based indexing).
becomes . The permutation that gives maximum beauty is . Hence, the answer is .

Editor Image

?