BABTWR - Tower of Babylon

no tags 

Apart from the Hanging Gardens the Babylonians (around 3000-539 b.c.) built the Tower of Babylon as well. The tower was meant to reach the sky, but the project failed because of a confusion of language imposed from much higher above.

For the 2638th anniversary a model of the tower will be rebuilt. n different types of blocks are available. Each one of them may be duplicated as many times as you like. Each type has a height y, a width x and a depth z. The blocks are to be stacked one upon eachother so that the resulting tower is as high as possible. Of course the blocks can be rotated as desired before stacking. However for reasons of stability a block can only be stacked upon another if both of its baselines are shorter.

Input

The number of types of blocks n is located in the first line of each test case. On the subsequent n lines the height yi, the width xi and the depth zi of each type of blocks are given. There are never more than 30 different types available.

There are many test cases, which come one by one. Input terminates with n = 0.

Edited: You can assume that max(xi, yi, zi) <= 2500.

Output

For each test case your program should output one line with the height of the highest possible tower.

Example

Sample input:
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
1
1 1 1
0

Sample output:
342
1

hide comments
ashish22_dwd: 2016-10-07 15:26:38

My 50th with this nice problem :)

hamjosh1: 2016-09-25 09:26:55

easy :'D

praval_singhal: 2016-06-30 13:24:20

AC in one go. Top-Down is great.

Mohit Rathore: 2015-08-29 10:00:17

This problem is just awesome!

agaurav77: 2014-12-31 15:49:11

This one is a nice DP question. Those who are confused about rotations, here's a hint : the number of rotations possible depend on the dimensions. For example, <1,1,1> has 1 rotation, whereas <1,2,3> has 6 rotations.

Rajat (1307086): 2014-12-29 05:04:23

Nelson number shows up with this DP. Do not cross check case 1. It is correct. use it to check your solution.

Archit Jain: 2014-12-24 20:21:04

nice dp

DEEPAK KUMAR SINGH: 2014-12-18 16:55:57

1st century for me on SPOJ....Completed 100 with a dp...

Last edit: 2014-12-18 16:56:35
mayank: 2014-12-15 10:04:58

DP! nice!

robert: 2013-04-23 03:08:59

Note: "a block can only be stacked upon another if both of its baselines are shorter"
This means block1 can go on block2 iff width1 < width2 and length1 < length2

It doesn't mean max(width1, length1) < min(width2, length2)

Last edit: 2013-04-23 03:09:55

Added by:MichaƂ Czuczman
Date:2004-07-06
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All
Resource:Swiss Olympiad in Informatics 2004