<p>Given <code>n</code> <code>cuboids</code> where the dimensions of the <code>i<sup>th</sup></code> cuboid is <code>cuboids[i] = [width<sub>i</sub>, length<sub>i</sub>, height<sub>i</sub>]</code> (<strong>0-indexed</strong>). Choose a <strong>subset</strong> of <code>cuboids</code> and place them on each other.</p>

<p>You can place cuboid <code>i</code> on cuboid <code>j</code> if <code>width<sub>i</sub> &lt;= width<sub>j</sub></code> and <code>length<sub>i</sub> &lt;= length<sub>j</sub></code> and <code>height<sub>i</sub> &lt;= height<sub>j</sub></code>. You can rearrange any cuboid&#39;s dimensions by rotating it to put it on another cuboid.</p>

<p>Return <em>the <strong>maximum height</strong> of the stacked</em> <code>cuboids</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/10/21/image.jpg" style="width: 420px; height: 299px;" /></strong></p>

<pre>
<strong>Input:</strong> cuboids = [[50,45,20],[95,37,53],[45,23,12]]
<strong>Output:</strong> 190
<strong>Explanation:</strong>
Cuboid 1 is placed on the bottom with the 53x37 side facing down with height 95.
Cuboid 0 is placed next with the 45x20 side facing down with height 50.
Cuboid 2 is placed next with the 23x12 side facing down with height 45.
The total height is 95 + 50 + 45 = 190.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cuboids = [[38,25,45],[76,35,3]]
<strong>Output:</strong> 76
<strong>Explanation:</strong>
You can&#39;t place any of the cuboids on the other.
We choose cuboid 1 and rotate it so that the 35x3 side is facing down and its height is 76.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> cuboids = [[7,11,17],[7,17,11],[11,7,17],[11,17,7],[17,7,11],[17,11,7]]
<strong>Output:</strong> 102
<strong>Explanation:</strong>
After rearranging the cuboids, you can see that all cuboids have the same dimension.
You can place the 11x7 side down on all cuboids so their heights are 17.
The maximum height of stacked cuboids is 6 * 17 = 102.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == cuboids.length</code></li>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= width<sub>i</sub>, length<sub>i</sub>, height<sub>i</sub> &lt;= 100</code></li>
</ul>

class Solution:
    def maxHeight(self, cuboids: List[List[int]]) -> int:
        # dp?
            # track last placed cuboid, and set of placed cuboids?
                # 2**100 states...
                # except for equal cuboids, placing is 1 directional, ie if A -> B then not B->A
                # so once we put a cuboid down, only its children can be stacked
                    # so dp could be on w,l / i, and remaining children?
                        # each cuboid has a unique set of remaining children, so there are only O(n) of these
                            # slightly more cuz of ordering, but that only applies to dupes
                                # so maybe could timeout if 100 dupes, but can probably special case that
            # if we treat is as a graph (can construct in n^2), we want the longest path
                # but somewhat complicated by rotation: cuboid height can change based on rotation
                # could maybe track 3 versions of each cuboid, store that graph (horiz rotations are trivial), then find longest path and ignore dupes/cycles
                    # longest path: bellman-ford?
                    # w,l,h; w,h,l; h,l,w
        n = len(cuboids)
        # for i in range(n):
        #     w0, l0, h0 = cuboids[i]
        #     for j in range(i+1, n):
        #         w1, l1, h1 = cuboids[j]
        cuboids = [sorted(c) for c in cuboids]

        @lru_cache(None)
        def rec(w, l, h, avail):
            # print(w,l,h,avail)
            best = 0
            avail_set = set()
            for i in range(n):
                if not (1<<i) & avail:
                    continue
            # for i in avail:
                [w1, l1, h1] = cuboids[i]
                # w=3, l=5, h=7
                if w1 > w or l1 > l or h1 > h:
                    # exclude irrelevant cuboids
                    avail &= ~(1<<i)
                    continue
                avail_set.add(i)
            # avail = frozenset(avail2)
            for i in avail_set:
                [w1, l1, h1] = cuboids[i]
                # avail2 = avail - {i}
                avail2 = avail & ~(1<<i)
                if (w1 <= w and l1 <= l and h1 <= h) or (w1 <= l and l1 <= w and h1 <= h):
                    # avail.remove(i)
                    best = max(best, h1 + rec(w1, l1, h1, avail2))
                    # avail.add(i)
                if (h1 <= w and l1 <= l and w1 <= h) or (h1 <= l and l1 <= w and w1 <= h):
                    # avail.remove(i)
                    best = max(best, w1 + rec(w1, l1, h1, avail2))
                    # avail.add(i)
                if (h1 <= w and w1 <= l and l1 <= h) or (h1 <= l and w1 <= w and l1 <= h):
                    # avail.remove(i)
                    best = max(best, l1 + rec(w1, l1, h1, avail2))
                    # avail.add(i)
            return best
        return rec(math.inf, math.inf, math.inf, 
        # frozenset(range(n))
        (1<<n) - 1
        )

19700