<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:
    void calculate_unbalance(vector<int>& nums, unordered_map<int, int>& hm)
    {
        int n = nums.size();
        for(int i = 0; i < (1 << n); ++i)
        {
            for(int j = i; ; j = (j - 1) & i)
            {
                int left_sum = 0, right_sum = 0;
                for(int k = 0; k < n; ++k)
                {
                    if((j & (1 << k)) != 0)
                        left_sum += nums[k];
                    else if((i & (1 << k)) != 0)
                        right_sum += nums[k];
                }
                hm[left_sum - right_sum] = max(hm[left_sum - right_sum], left_sum);
                if(j == 0)
                    break;
            }
        }
    }
    int tallestBillboard(vector<int>& rods) 
    {
        int n = rods.size();
        vector<int> left, right;
        unordered_map<int, int> left_map, right_map;
        for(int i = 0; i < n / 2; ++i)
            left.push_back(rods[i]);
        for(int i = n / 2; i < n; ++i)
            right.push_back(rods[i]);
        calculate_unbalance(left, left_map);
        calculate_unbalance(right, right_map);
        int ans = 0;
        for(auto ele : left_map)
        {
            if(right_map.find(-ele.first) != right_map.end())
                ans = max(ans, ele.second + right_map[-ele.first]);
        }
        return ans;

    }
};
21534