Welcome to ZOJ
Select Problem
ZOJ Problem Set - 2683

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A team marathon (relay race) is held in Seoul. The team marathon rules are as follows:

  • Each team consists of exactly N runners.
  • The winner is the team that runs the longest distance for D days.
  • For each day, exactly one runner from each team should run.
  • If a team member runs on a particular day, that team member should run for the entire day. That is, runner exchanges are only possible at the beginning of each day.
  • Each of the N runners on a team should run for at least one day, but at most three days.
  • If a runner participates in running for a day or for a sequence of days, and then, is switched with another runner, he/she cannot participate in the marathon again.

A team, called chic-chic-pok-pok, joins the team marathon. The coach of the team has already determined the order in which his N runners will run in D days, but has not yet decided the exact number of days each runner should run. Now, he will make that determination by examining each runner's prior running record. The running record of each runner contains a list of three numbers. The i-th (i = 1, 2, 3) number indicates the distance (in kilometers) he/she can run for a period of i consecutive day(s). For example, if the running record of a runner is (4, 7, 9), the coach assumes that the runner can run distances of 4 kilometers in one day, 7 kilometers in two days, and 9 kilometers in three days. Note that if (a, b, c) is the running record of a runner, it should satisfy that a <= b <= c.

Now, the coach wants to know the longest distance his team can run for D days. For example, suppose N = 3, D = 4, and the running records of the first, second, and third runners on team chic-chic-pok-pok are (4, 7, 8), (2, 4, 6), and (4, 5, 6), respectively. Then, the longest distance the team can run in 4 days is 13 kilometers; The first runner runs 7 km in two days (i.e., the first and the second days), the second runs 2 km in a single day (i.e., the third day), and the third runs 4 km in a single day (i.e., the fourth day).

Write a program that returns the longest distance the team can run in D days.


The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case starts with a line containing an integer N, the number of runners on team-chic-chic-pok-pok, 1 <= N <= 50. The next line contains an integer D, the number of days in the marathon, 1 <= D <= 150. The next N lines contain the runners�� running records, one for each runner, from the first runner to the last runner.


Print exactly one line for each test case. The line is to contain the integer that is the longest distance the team can run in D days. If it is impossible to calculate the distance due to a lack of or an inconsistency in the running records, or if there is a violation of the marathon rules, the line is to contain ?1. The following shows sample input and output for two test cases.

Sample Input

4 7 8
2 4 6
4 5 6
2 3 5
3 6 8

Sample Output


Source: Asia 2003, Seoul (South Korea)
Submit    Status