<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 tallestBillboard(vector<int>& v) {
        int n = v.size();
        int x = n/2;
        map<int,int>lft,rht;
        for(int mask=0;mask<=((1<<x)-1);mask++){
            for(int j=0;j<x;j++){
                if((1<<j)&mask){
                    lft[mask]+=v[j];
                }
            }
        }
        x = n-x;
        for(int mask=0;mask<=((1<<x)-1);mask++){
            for(int j=n/2;j<n;j++){
                if((1<<(j-n/2))&mask){
                    rht[mask]+=v[j];
                }
            }
        }
        x = n/2;
        map<int,int>p1,p2;
        for(int mask = 0; mask < (1<<x); mask++){
            int cur = mask;
            cur = ((1<<x)-1)^mask;
            int m=cur;
            while(cur>=0){
                p1[lft[mask]-lft[cur]]=max(p1[lft[mask]-lft[cur]],lft[mask]);
                if(cur==0)break;
                cur = (cur-1)&m;
            }
        }
        x=n-x;
        for(int mask = 0;mask<(1<<x);mask++){
            int cur=mask^((1<<x)-1);
            int m = cur;
            while(cur>=0){
                p2[rht[mask]-rht[cur]]=max(p2[rht[mask]-rht[cur]],rht[mask]);
                if(cur==0)break;
                cur = (cur-1)&m;
            }
        }
        int ans = 0;

        for(auto it:p1){
            if(p2.find(-1*it.first)!=p2.end()){
                ans = max(ans,it.second+p2[-1*it.first]);
            }
        }
        return ans;
    }
};
29690