This page is still under construction.

Parts of this page are still being built. What you see may change.

The Tower of Babylon

Time limit1sMemory limit128 MB

Summary
Given block types with unlimited copies, each reorientable, find the maximum height of a stack where each block's base is strictly smaller than the one below.
Level

Medium6 of 10

Topics
Dynamic programming, Sorting, Graph
Solved
No attempts yet

Problem

The Babylonians had nn types of blocks, with an unlimited supply of blocks of each type. A type-ii block is a rectangular solid with side lengths (xi,yi,zi)(x_i, y_i, z_i). A block may be reoriented so that any two of its three sides form the base and the remaining side becomes its height.

You want to stack these blocks to build the tallest possible tower. A block may be placed on top of another only if both base side lengths of the upper block are strictly smaller than the corresponding base side lengths of the lower block. In particular, two blocks whose bases have equal dimensions cannot be stacked on each other.

Write a program that determines the height of the tallest tower that can be built from a given set of blocks.

Input

The input consists of one or more test cases. The first line of each test case contains an integer nn, the number of block types (the maximum value of nn is 30). Each of the next nn lines contains three integers xix_i, yiy_i, and ziz_i.

The input is terminated by a line containing n=0n = 0.

Output

For each test case, print one line containing the case number (numbered sequentially starting from 1) and the height of the tallest possible tower, in the format

Case c: maximum height = h

Examples1

  1. Example 1

    Input
    1
    10 20 30
    2
    6 8 10
    5 5 5
    7
    1 1 1
    2 2 2
    3 3 3
    4 4 4
    5 5 5
    6 6 6
    7 7 7
    5
    31 41 59
    26 53 58
    97 93 23
    84 62 64
    33 83 27
    0
    
    Expected output
    Case 1: maximum height = 40
    Case 2: maximum height = 21
    Case 3: maximum height = 28
    Case 4: maximum height = 342