Under construction

Still being built. Things may change

Animesh practices some programming contests

Time limit1sMemory limit1024 MB

Summary
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 TEiTE_i hours and gives PEiPE_i pleasure. The medium problem takes TMiTM_i hours and gives PMiPM_i pleasure. The hard problem takes THiTH_i hours and gives PHiPH_i 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 KK 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 timetime 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 TT, the number of test cases.

For each test case, the first line contains three space-separated integers nn, kk, and timetime. The next nn lines each contain three space-separated integers TEiTE_i, TMiTM_i, THiTH_i. The following nn lines each contain three space-separated integers PEiPE_i, PMiPM_i, PHiPH_i.

Output

For each test case, output a single integer on its own line, the answer to the problem.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤n≤501 \le n \le 50
  • 0≤k≤n20 \le k \le n^2
  • 1≤time≤501 \le time \le 50
  • 1≤TEi,TMi,THi≤501 \le TE_i, TM_i, TH_i \le 50
  • 1≤PEi,PMi,PHi≤1000001 \le PE_i, PM_i, PH_i \le 100000

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.

Examples1

  1. Example 1

    Input
    2
    1 0 5
    1 2 3
    5 3 6
    2 1 6
    1 2 1
    2 3 3
    5 3 3
    4 6 5
    
    Expected output
    6
    11