Miss Great TOI is a famous pageantry that seeks representatives from all provinces to compete at the national competition. For this, CEO Nawat Thattong personally holds the Miss Phit'lok competition to find the representative of Phitsanulok province. The top two competitors Nu-Kee and Non-Um are in a tight race. Nu-Kee wants to sabotage Non-Um by giving her a devious gift: a piece of fruit laced with poison. Non-Um has no choice but to accept and eat it out of politeness. Not wanting the situation to be extreme, Nu-Kee uses poison that only causes mild sickness. She also puts antidote in other pieces of fruit, just in case Non-Um is too ill to get on stage at all, she can play the role of a heroine. What a classic pageantry drama! It is so classic that other competitors also put poison and antidotes of multiple recipes in pieces of fruit at the pageantry, and therefore a piece of fruit may contain poison of more than 1 recipe and antidotes of more than 1 recipe. Nevertheless, Non-Um is experienced in the pageantry business, and she asks her mentor to find out which poison and antidotes are present in which pieces of fruit. The mentor advises Non-Um about the way to relief herself from poison as follows:
The Miss Phit'lok pageantry war is full of twists and turns. At one point, Nu-Kee sees a text message sent to Non-Um to warn about the fiasco. Nu-Kee is now aware that Non-Um knows the way to relieves herself from poisoning, but Nu-Kee cannot find an alternate plan, so she remains committed to the plan to poison her opponent. However, she wants to choose a piece of fruit for Non-Um to eat so that, in order to cure the poisoning, she must consume other pieces of fruit in such a way that the total weight is as large as possible.
Example 1
Assume the pageantry has 5 pieces of fruit and 3 recipes of poison, with the details as shown in Table 1.
| fruit | weight (units) | antidote | poison | ||||
| recipe 1 | recipe 2 | recipe 3 | recipe 1 | recipe 2 | recipe 3 | ||
| 1 | 1 | - | - | - | yes | yes | - |
| 2 | 1 | yes | - | - | - | - | - |
| 3 | 1 | - | - | - | - | - | yes |
| 4 | 1 | - | - | yes | - | yes | - |
| 5 | 1 | - | yes | - | yes | - | - |
For the example above, Nu-Kee chooses to hand Non-Um fruit 3 for her to eat because Non-Um the fruits she must eat to get her to complete relief has the largest total weight.
Example 2
Assume the pageantry has 5 pieces of fruit and 3 recipes of poison, with the details as shown in Table 2.
| fruit | weight (units) | antidote | poison | ||||
| recipe 1 | recipe 2 | recipe 3 | recipe 1 | recipe 2 | recipe 3 | ||
| 1 | 1 | - | - | - | yes | yes | - |
| 2 | 3 | - | yes | - | - | - | - |
| 3 | 4 | yes | yes | - | - | - | yes |
| 4 | 5 | yes | yes | yes | - | - | - |
| 5 | 7 | - | - | yes | - | - | - |
If Nu-Kee hands Non-Um fruit 1 for her to eat, Non-Um receives poison of recipe 1 and recipe 2 immediately
Your Task
Write an efficient program to determine, from the fruit data that contains the number of fruits, their weights, and the presence of poison/antidote in each piece of fruit, the total weight of fruit that Non-Um eats to complete relief herself, under the following conditions:
There are $$$N+1$$$ lines of input:
| Line 1 | 2 integers, separated by a space. The first integer is $$$N$$$, denoting the number of pieces of fruit, where $$$1\leq N\leq 80{,}000$$$. The second integer is $$$S$$$, denoting the number of recipes of poison, where $$$1\leq S\leq 19$$$. |
| Line $$$i+1$$$ to $$$N+1$$$ for $$$i=1,\ldots,N$$$ | $$$S+1$$$ integers, separated by a space. The first integer is $$$w_i$$$, denoting the weight of fruit $$$i$$$ for $$$1\leq w_i\leq 1{,}000$$$. Then, integer $$$p_i^j$$$ ($$$j=1,\ldots,S$$$) denotes that fruit $$$i$$$ contains poison or antidotes of which recipes, as follows:
|
There is 1 line of output:
| Line 1 | 1 integer denoting the total weight of fruit that is the smallest possible that Non-Um eats for complete relief under the condition that Nu-Kee chooses the piece of fruit with poison that leads to Non-Um eating pieces of fruit with antidote for complete relief with the largest total weight. If Non-Um does not receive a piece of fruit containing poison, answer 0. |
Notices regarding the test sets are as follows:
| Test Group | Maximum attainable score in this group | Condition(s) |
| 1 | 9 | $$$N\leq 8$$$ |
| 2 | 4 | Each piece of fruit containing poison has only 1 other piece of fruit that contains the matching antidote for it. Each piece of fruit contains an antidote for at most 1 other piece of fruit. In addition, $$$N\leq 200$$$ |
| 3 | 6 | Each piece of fruit containing poison has only 1 other piece of fruit that contains the matching antidote for it, except for one special piece of fruit that, for the poison in it, there are more than 1 piece of fruit that contains the antidote for it. However, it is still guaranteed that each piece of fruit contains an antidote for at most 1 other piece of fruit. In addition, $$$N\leq 200$$$ |
| 4 | 9 | $$$N\leq 5{,}000$$$ and every piece of fruit has weight 1 |
| 5 | 8 | $$$N\leq 700$$$ |
| 6 | 7 | $$$N\leq 1{,}500$$$ |
| 7 | 22 | $$$N\leq 5{,}000$$$ |
| 8 | 11 | $$$S\leq 12$$$ |
| 9 | 24 | No additional constraints |
4 2 5 0 1 6 -1 1 7 1 0 8 -1 -1
7
5 3 1 -1 -1 0 1 1 0 0 1 0 0 -1 1 0 -1 1 1 -1 1 0
3
Recommended programming tips
If the contestant uses cin/cout, it is recommended to add 2 lines as the following:
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
| Name |
|---|


