## NOI '97 P5 - Building Game

View as PDF

Points: 15 (partial)
Time limit: 0.6s
Memory limit: 16M

Problem type
##### National Olympiad in Informatics, China, 1997

SERCOI has recently designed a game with building block. Each player has wooden blocks (with IDs assigned from 1 to ) in the shape of rectangular prisms. For every block, its three distinct sides are respectively known as "side a", "side b" and "side c", as shown in the figure below:

The rules of the game are as follows:

1. Each player selects some number of blocks from their total blocks, and splits them up into piles which we will call pile 1, pile 2, …, pile . Each pile must have at least 1 block, and the ID of any block in the -th pile must be greater than the ID of any block in the ()-th pile (for ).
2. For each pile of blocks, the player must arrange them into a stack under the following two conditions:
• Other than the top-most block in the stack, the top surface of every block must be touching the bottom surface of another block. Additionally, for every pair of touching blocks, the top block's bottom surface must be fully contained in the bottom block's top surface. In other words, the dimensions of the bottom block's top surface must be larger than or equal to the corresponding dimensions of the top block's bottom surface.
• For every pair of touching blocks, the ID of the lower block must be smaller than the ID of the upper block.

At the end, the winner is determined from the heights of each player's stacks. Please write a program that finds a strategy for stacking blocks such that the sum of the heights of the stacks are maximized.

#### Input Specification

The first line of input contains two space-separated integers and , the number of blocks and the number of stacks to build, respectively. The next lines each contain three side lengths , , and , each an integer from 1 to 1000, describing the dimensions of the blocks.

#### Output Specification

The output should contain one line with one integer - the largest possible sum of the heights of the stacks.

#### Sample Input

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

#### Sample Output

24

Problem translated to English by Alex.