## BdOI 2013 National Problem 4

Discuss everything related to IOI here. For more general or advanced topics use CS forum.

Moderators: bristy1588, Labib

bristy1588
Posts: 92
Joined: Sun Jun 19, 2011 10:31 am

### BdOI 2013 National Problem 4

This is the hardest level of the game. There are N stages in the level. In each stage you will
be given \$ Q_i \$ balls. Colors of the balls are denoted by an integer between \$1\$ and \$K\$. You can
pick up one ball from each stage or skip it (No ball). If you pick up a ball you will gain or lose
some points depending on the last ball you have picked. You know color of the available
balls of each stage and points you'll get for a certain combination. Can you figure out the
maximum point you can get? Note that you wont gain or lose any point for the first ball you
picked. If you skip a stage, you can never go back to that. Means you need to play in order.

INPUT
First line of input will contain number of test cases \$ T (T < 150) \$. In each case first line will
contain two integers \$ N (2 <= N <= 100) \$ and \$ K (1 <= K <= 100)\$. Then there will be N lines
which describe each stage. Each line contains an integer \$ Q_i (0 <= Q_i <= 100)\$ which denotes
number of balls available in \$i^{th} \$ stage and then \$ Q_i \$ integers will denote the colors of the balls
in that stage. Then there will be \$ K \$ lines, each containing \$ K \$ integers, \$ j^{th} \$ integer of ith
line will indicate the point you will gain or lose if you pick a ball of color \$ j\$ after color \$i\$. Negative
means you will lose point. Point gain or lose in a step will be at most \$1000\$.

OUTPUT
For each test case, print the test case number starting from \$1\$ and a single integer denoting
the maximum point you can get.

SAMPLE INPUT
SAMPLE OUTPUT
Bristy Sikder