<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:
        # state reduction: https://leetcode.com/problems/tallest-billboard/solutions/1974364/c-dp-memoization-most-intuitive-optimizing-dp-states/?envType=list&envId=rfqlj0e6
        # origin states: (position, s1, s2)
        #O(mn) m:max  O(mn)


        @cache
        def dp(i, d): #difference
            if i == len(rods): 
                return 0 if d == 0 else -inf
            op1 = dp(i + 1, d)                        # do nothing
            op2 = rods[i] + dp(i + 1, d + rods[i])    # pick rod for left one
            op3 = dp(i + 1, d - rods[i])              # pick rod for right one
            return max(op1,  op2,  op3) # op2 is tracking the length
        return dp(0, 0)



        @cache
        def dp(i, r1, r2): #difference
            if i == len(rods): 
                if r1 == r2:
                    return r1
                else: 
                    return 0
                
            op1 = dp(i+1, r1, r2)
            op2 = dp(i+1, r1+rods[i], r2)
            op3 = dp(i+1, r1, r2+rods[i])
            return max(op1, op2, op3)
        return dp(0, 0, 0)












        # bottom up dp
        # meet in the middle O(3^N) => 2*O(3^(n/2))
        # dp[taller - shorter] = dp[diff] = taller
        # O(nm) m: max sum of rods, O(m)
        # dp[taller - shorter] = taller
        dp = collections.defaultdict(int)
        dp[0] = 0
        
        for r in rods:
            # dp.copy() means we don't add r to these stands.
            new_dp = dp.copy()
            for diff, taller in dp.items():
                shorter = taller - diff
                
                # Add r to the taller stand, thus the height difference is increased to diff + r.
                new_dp[diff + r] = max(new_dp[diff + r], taller + r)
                
                # Add r to the shorter stand, the height difference is expressed as abs(shorter + r - taller).
                new_diff = abs(shorter + r - taller)
                new_taller = max(shorter + r, taller)
                new_dp[new_diff] = max(new_dp[new_diff], new_taller)
                
            dp = new_dp
            
        # Return the maximum height with 0 difference.
        return dp[0]
257800