<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 dfsRodLengths(vector<int>& rods, unordered_map<int, int>& rod_diff_map, int i, int sum1, int sum2){
        if(i >= rods.size()){
            int length_diff = sum1 - sum2;
            if (!rod_diff_map.contains(length_diff)){
                rod_diff_map[length_diff] = sum1;
            }
            else{
                if(sum1 > rod_diff_map[length_diff]){
                    rod_diff_map[length_diff] = sum1;
                }
            }
        }
        else{
            dfsRodLengths(rods, rod_diff_map, i+1, sum1 + rods[i], sum2);
            dfsRodLengths(rods, rod_diff_map, i+1, sum1, sum2 + rods[i]);
            dfsRodLengths(rods, rod_diff_map, i+1, sum1, sum2);
        }
    }

    // void printRodDiffMap(unordered_map<int, vector<int>>& rod_diff_map){
    //     for(auto it : rod_diff_map){
    //         int diff = it.first;
    //         vector<int> constructs = it.second;
    //         cout<<diff<<" : ";
    //         for(int i = 0; i < constructs.size(); i++){
    //             cout<<constructs[i]<<", ";
    //         }
    //         cout<<"\n";
    //     }
    // }
    void printRods(vector<int>& rods){
        for(int i = 0; i < rods.size(); i++){
            cout<<rods[i]<<",";
        }
        cout<<"\n";
    }

    int tallestBillboard(vector<int>& rods) {
        unordered_map<int, int> rod_diff_map1, rod_diff_map2;
        int n = rods.size();
        vector<int> rods1(rods.begin(), rods.begin() + (n/2));
        vector<int> rods2(rods.begin() + (n/2), rods.end());

        dfsRodLengths(rods1, rod_diff_map1, 0, 0, 0);
        dfsRodLengths(rods2, rod_diff_map2, 0, 0, 0);

        // printRods(rods1);
        // printRodDiffMap(rod_diff_map1);
        // cout<<"\n";
        // printRods(rods2);
        // printRodDiffMap(rod_diff_map2);
        int ans = 0;

        for(auto it : rod_diff_map1){
            int diff = it.first, rod1 = it.second;
            if(rod_diff_map2.contains(-diff)){
                ans = max(ans, rod_diff_map2[-diff] + rod1);
            }
        }

        
        return ans;
    }
};
24253