# Birthday Gifts solution codechef

## Birthday Gifts solution codechef

Table of Contents

It’s Chef’s birthday. He and his twin have received NN gifts in total. The ii-th gift has a price of AiAi. Each twin wants to keep the most expensive gifts for himself.

The twins take KK turns alternately (each has KK turns, for 2K2⋅K turns in total). It is given that 2K+1N2⋅K+1≤N. In a turn, a person may choose one gift. The only exception is the last turn of the twin who moves second, where he gets to choose two gifts instead of one. Assuming they both choose gifts optimally and you can choose who takes the first turn, find the maximum total cost of the gifts that Chef keeps.

### Input Birthday Gifts solution codechef

• The first line contains an integer TT, the number of test cases. Then the test cases follow.
• Each test case contains two lines of input.
• The first line contains two space-separated integers NNKK.
• The second line contains NN space-separated integers A1,A2,,ANA1,A2,…,AN, the price of the gifts.

### Output Birthday Gifts solution codechef

For each test case, output in a single line the answer to the problem.

### Constraints Birthday Gifts solution codechef

• 1T1001≤T≤100
• 3N1033≤N≤103
• 1KN121≤K≤N−12
• 1Ai1091≤Ai≤109

### Subtasks Birthday Gifts solution codechef

Subtask #1 (100 points): original constraints

### Sample Input Birthday Gifts solution codechef

3
3 1
1 3 2
3 1
3 1 3
5 2
5 1 3 2 4


### Sample Output Birthday Gifts solution codechef

3
4
8


### Explanation Birthday Gifts solution codechef

Test Case 11: Chef moves first and he chooses the gift with cost 33. His twin then chooses the gifts of costs 11 and 22.

Test Case 22: Chef allows his brother to choose first and his brother chooses a gift with cost 33. Then Chef chooses the remaining gift with cost 33. Since Chef moves second, he is allowed to choose one more gift, so he chooses gift with cost 11. The total cost of Chef’s gifts is 3+1=43+1=4.

Test Case 33: Chef moves first and at the end he will have the gifts with costs 55 and 33. Hence, the total cost of gifts with Chef = 5+3=85+3=8.

# Click here

#### What’s in it for you?

The idea behind these programming contests is that we want you to learn while competing. Also we believe that it is alright to refer to tutorials, books and other materials, learn a concept and then apply the same to solve a problem during a contest. But it is not alright to copy other people’s solutions or seek other people’s help to solve a problem without understanding it.

The dividing line may seem to be thin but it can be captured by the spirit of learning. If whatever you are doing is making you learn while you do so, we tend to believe that it is alright. Our sole intention lies in making our users learn new concepts while competing. However, all the participants are expected to abide to

#### What’s new in CodeChef?

You may now play with our new groups & LaTex feature on discuss.

• Please do not discuss strategy, suggestions or tips in the comments during a live contest. Posting questions clarifying the problem statement is ok. If you are unsure, email us at [email protected] .
• Discussing CodeChef’s problems or any aspect of problem, on any other platform on web, on identification, could lead to disabling of respective account and banning from the community.
• You can also send in your queries in an email to [email protected], during the contest.
• You will receive one point for solving a problem (passing all test cases – no partial credit), regardless of the level of difficulty of that problem.
• Users are ranked according to the most problems solved. Ties will be broken by the total time for each user in ascending order of time.
• The total time is the sum of the time consumed for each problem solved. The time consumed for a solved problem is the time elapsed from the beginning of the contest to the submittal of the first accepted run plus 10 penalty minutes for every previously rejected run for that problem. There is no time consumed for a problem that is not solved.
• You shall not possess more than one account on CodeChef. If you have, then do let us know, so that we can deactivate the insignificant one. If you do not report it and we come to know about it, we may deactivate both the accounts permanently.
• If anyone is using code from some other source in his submission, he should provide proper attribution. Failing this, it may be considered plagiarism and the submission will be subject to disqualification.
• The number of submissions that one can make during the contest on a single problem will be limited to 500.
• Residents of the following countries and territories are not eligible to win cash prizes/laddus/goodies due to legal restrictions: Albania, The Bahamas, Barbados, Botswana, Cambodia, Crimea region of Russia, Cuba, Ghana, Iceland, Iran, Jamaica, Mauritius, Mongolia, Myanmar, Nicaragua, North Korea, Pakistan, Panama, Sudan, Syria, Uganda, Yemen, Zimbabwe.

Note: You can now “Code, Compile, and Run” your codes on our Online IDE.

However, if you are using any other online development environment, make sure that other contestants don’t have access to your code. As a contestant, you are responsible for making sure others don’t access the code that you submit. If you use Ideone, make sure to mark your submission “private” (not secret)”.