Animesh practices some programming contests
Time limit1sMemory limit1024 MB
Pick at most one problem per contest within the time limit, using at most k swaps between contests, to maximize total pleasure.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Bit manipulation
- Solved
- No attempts yet
Problem
Malvika has created n programming contests. Each contest has three problems, categorized as easy, medium, and hard. For the i-th contest, the easy problem takes hours and gives pleasure. The medium problem takes hours and gives pleasure. The hard problem takes hours and gives pleasure.
Today, Animesh wants to practice some of them. He has a bad habit of trying problems for only a few minutes and then telling Malvika "I am a noob, you are a pro. It's some weird shit I don't know. Please, tell me the solution, bro!" Malvika got irritated by this many times, so she granted him special powers he can use before starting the practice session. With one power, Animesh can pick any two problems, regardless of difficulty, from two different contests and swap them.
Animesh has at most hours before he gets frustrated and ends the session. He wants to get as much pleasure as possible in that time. He also gets bored with contest themes quickly, so he does not want to solve more than one problem from any contest. Find the maximum amount of pleasure Animesh can get.
Input
The first line contains a single integer , the number of test cases.
For each test case, the first line contains three space-separated integers , , and . The next lines each contain three space-separated integers , , . The following lines each contain three space-separated integers , , .
Output
For each test case, output a single integer on its own line, the answer to the problem.
Constraints
Hint
In the first example, Malvika has prepared only one contest. Its easy problem takes 1 hour and gives 5 units of pleasure. The medium problem takes 2 hours and gives 3 units. The hard problem takes 3 hours and gives 6 units. Animesh has only 5 hours and can solve at most one of these problems. The hard problem is the best choice, since it gives him 6 units of pleasure and takes his 5 hours. So the answer is 6.
In the second example, Malvika has prepared two contests.
First contest: easy takes 1 hour and gives 2 units, medium takes 2 hours and gives 3 units, hard takes 1 hour and gives 3 units.
Second contest: easy takes 2 hours and gives 4 units, medium takes 3 hours and gives 6 units, hard takes 3 hours and gives 5 units.
Animesh has 6 hours and can use the special power at most once. He swaps the medium problem of the second contest with any problem of the first contest. Then he can select the two problems that take 3 hours each and give 6 and 5 units, for a total of 11 units.