<p>You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.</p>

<p>You are given a collection of <code>rods</code> that can be welded together. For example, if you have rods of lengths <code>1</code>, <code>2</code>, and <code>3</code>, you can weld them together to make a support of length <code>6</code>.</p>

<p>Return <em>the largest possible height of your billboard installation</em>. If you cannot support the billboard, return <code>0</code>.</p>

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

<pre>
<strong>Input:</strong> rods = [1,2,3,6]
<strong>Output:</strong> 6
<strong>Explanation:</strong> We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.
</pre>

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

<pre>
<strong>Input:</strong> rods = [1,2,3,4,5,6]
<strong>Output:</strong> 10
<strong>Explanation:</strong> We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.
</pre>

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

<pre>
<strong>Input:</strong> rods = [1,2]
<strong>Output:</strong> 0
<strong>Explanation:</strong> The billboard cannot be supported, so we return 0.
</pre>

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

<ul>
	<li><code>1 &lt;= rods.length &lt;= 20</code></li>
	<li><code>1 &lt;= rods[i] &lt;= 1000</code></li>
	<li><code>sum(rods[i]) &lt;= 5000</code></li>
</ul>

class Solution:
    def tallestBillboard(self, rods: List[int]) -> int:
        n = len(rods)

        res = []
        for i in range(n):
            t = []
            for j in range(10001):
                t.append(-1)
            res.append(t)

        res[0][5000 + 0] = 0
        res[0][5000 - rods[0]] = rods[0]
        res[0][5000 + rods[0]] = rods[0]

        for i in range(1, n):
            for j in range(10001):
                if res[i-1][j] != -1:

                    if j == 5000: # here both rods have same height
                        left = right = res[i-1][j]
                        res[i][j + 0] = max(res[i][j + 0], left)
                        res[i][j + rods[i]] = max(res[i][j + rods[i]], right + rods[i])
                        res[i][j - rods[i]] = max(res[i][j - rods[i]], left + rods[i])

                    elif j < 5000: # here left rod is taller
                        left = res[i-1][j]
                        diff = 5000 - j
                        right = left - diff
                        res[i][j + 0] = max(res[i][j + 0], left)
                        res[i][j + rods[i]] = max(res[i][j + rods[i]], max(right + rods[i], left))
                        res[i][j - rods[i]] = max(res[i][j - rods[i]], left + rods[i])

                    else: # here right rod is taller
                        right = res[i-1][j]
                        diff = j - 5000
                        left = right - diff
                        res[i][j + 0] = max(res[i][j + 0], right)
                        res[i][j + rods[i]] = max(res[i][j + rods[i]], right + rods[i])
                        res[i][j - rods[i]] = max(res[i][j - rods[i]], max(left + rods[i], right))

        return res[n-1][5000]

                    

19300