<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 {
public:
    int tallestBillboardV2(vector<int>& rods) {       // Works - but not optimal
        int n = rods.size(), res = 0, masks = (1 << n);
        unordered_map<int, int> sums;

        for(int i = masks - 1; i >= 0; i--){
            int count = 0, sum = 0;

            for(int j = 0; j < n; j++){
                if(i & (1 << j)) sum += rods[j];
            }

            if(sums.find(2 * sum) != sums.end() and ((sums[2 * sum] & i) == i)) 
                res = max(res, sum);
                // return sum;
            sums[sum] = i;      // this represents which bits are set for the calculated sum
        }

        return res;
    }

    vector<vector<int>> dp;
    int explore(int i, int sum1, int sum2, int maxSum, vector<int> &rods) {
        if(i >= rods.size()) return sum1 == sum2 ? sum1 : -2;
        if(sum1 > maxSum or sum2 > maxSum) return -2;

        if(dp[i][abs(sum1 - sum2)] != -1) 
            return dp[i][abs(sum1 - sum2)] == -2 ? -2 : dp[i][abs(sum1 - sum2)] + max(sum1, sum2);

        // take it for sum1
        int option1 = explore(i + 1, sum1 + rods[i], sum2, maxSum, rods);
        // take it for sum2
        int option2 = explore(i + 1, sum1, sum2 + rods[i], maxSum, rods);
        // dont take it
        int option3 = explore(i + 1, sum1, sum2, maxSum, rods);

        int total = max(option1, max(option2, option3));
        dp[i][abs(sum1 - sum2)] = (total == -2 ? -2 : total - max(sum1, sum2));
        return total;
    }

    int tallestBillboard(vector<int>& rods) {
        int maxSum = accumulate(rods.begin(), rods.end(), 0) / 2;
        dp.resize(rods.size(), vector<int> (2 * maxSum, -1));
        return explore(0, 0, 0, maxSum, rods);
    }
};

// [104,104,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,99,98]
29690