Timer is Running

You are given a set of **N** types of rectangular 3-D boxes, where the ith box has height **h**, width **w** and length **l**. You task is to create a stack of boxes which is as tall as possible, but you can only stack a box on top of another box if the dimensions of the 2-D base of the lower box are each strictly larger than those of the 2-D base of the higher box. Of course, you can rotate a box so that any side functions as its base.It is also allowable to use multiple instances of the same type of box. You task is to complete the function **maxHeight** which returns the height of the highest possible stack so formed.

**Input:**

The first line of input contains an integer **T** denoting the number of test cases then T test cases follow. Each test case contains an integer N denoting the total no of boxes available. In the next line are 3*N space separated values denoting the height, width and length of the N boxes.

**Output:**

For each test case in a new line output will be the **highest possible stack height** which could be formed.

**Expected Time Complexity** : O(N*N)

**Expected Auxiliary Space**: O(N)

**Constraints:**

1<=T<=100

1<=N<=100

1<=l,w,h<=100

**Example (To be used for expected output) :
Input:**

2

4

4 6 7 1 2 3 4 5 6 10 12 32

3

1 2 3 4 5 6 3 4 1

**Output**

60

15

**Explanation:
Test Case 1:**

One way of placing the boxes is as follows in the bottom to top manner:

(Denoting the boxes in (l, w,

(12, 32,

Hence, the total height of this stack is 10 + 32 + 4 + 4 + 6 + 1 + 3 = 60.

No other combination of boxes produces a height greater than this.

**Note: **

Base of the lower box should be strictly larger than that of the new box we're going to place. This is in terms of both length and width, not just in terms of area. So, two boxes with same base cannot be placed one over the other.

**Note: **The **Input/Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

sudhanmadhu7981 | 631 |

RudraKaniya | 624 |

ishaanprajapati808 | 620 |

saxenadivyanshi004 | 572 |

AmanSharma44 | 563 |

sudhanmadhu7981 | 631 |

RudraKaniya | 629 |

ishaanprajapati808 | 620 |

saxenadivyanshi004 | 572 |

AmanSharma44 | 563 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4736 |

Login to report an issue on this page.