#P5760. [NOI1997] 积木游戏

[NOI1997] 积木游戏

Description

SERCOI has recently designed a block game. Each player has NN rectangular blocks numbered 1,2,,N1, 2, \ldots, N. For each block, its three different edges are called the "a edge", "b edge", and "c edge", as shown in the figure below:

The rules are as follows:

  1. Choose some blocks from the NN blocks and divide them into MM (1MN1 \le M \le N) piles, called pile 11, pile 22, \ldots, pile MM. Each pile must contain at least 11 block, and for pile KK, the number of any block in pile KK must be greater than the number of any block in pile K+1K+1 (2KM2 \le K \le M).

  2. For each pile, the player stacks the blocks vertically into a single column, and it must satisfy the following two conditions:

\qquad 1) Except for the top block, the top face of any block must touch one and only one other block’s bottom face. Also, the top face of the lower block must be able to contain the bottom face of the upper block. That is, the lengths of the two pairs of edges on the top face of the lower block must each be greater than or equal to the corresponding two pairs of edges on the bottom face of the upper block.

\qquad 2) For any two blocks whose top and bottom faces are in contact, the number of the lower block must be smaller than the number of the upper block.

Finally, the winner is determined by the sum of the heights of the MM columns.

Please write a program to find a stacking plan such that the sum of the heights of the MM columns is maximized.

Input Format

The first line contains two positive integers NN and MM (1MN1001 \le M \le N \le 100), representing the total number of blocks and the required number of columns. The two numbers are separated by a space.

The next NN lines give the sizes of the NN blocks numbered from 11 to NN in order. Each line contains three integers between 11 and 10001000, representing the lengths of the a edge, b edge, and c edge of that block. Adjacent numbers on the same line are separated by a space.

Output Format

Only one line containing one integer, which is the maximum possible sum of the heights of the MM columns.

4 2
10 5 5
8 7 7
2 2 2
6 6 6

24

Hint

Translated by ChatGPT 5