<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 {
//     int ans = 0;
//     void solve(vector<int> &rods,int sum1,int sum2,int ind){
//         int n =rods.size();
//         if(sum1==sum2) ans=max(ans,sum1);
//         if(ind==n) return;
//         solve(rods,sum1,sum2,ind+1);
//         solve(rods,sum1+rods[ind],sum2,ind+1);
//         solve(rods,sum1,sum2+rods[ind],ind+1);
//     }
//     vector<vector<int>> dp;
//     int solve(vector<int> &rods,int diff,int ind){
//         int n =rods.size();
//         if(ind==n){
//             if(diff==0) return 0;
//             return -1e9;
//         }
//         if(dp[ind][abs(diff)]!=-1) return dp[ind][abs(diff)];
//         int x = solve(rods,diff,ind+1);
//         int y = rods[ind] + solve(rods,diff+rods[ind],ind+1);
//         int z = rods[ind] + solve(rods,diff-rods[ind],ind+1);
//         return dp[ind][abs(diff)] =max(x,max(y,z)); 
//     }  
// public:
//     int tallestBillboard(vector<int>& rods) {
//         int n =rods.size();
//         int sum = accumulate(rods.begin(),rods.end(),0);
//         dp.resize(n+1,vector<int>(sum,-1));
//         return solve(rods,0,0)/2;
//     }
// };
class Solution {
public:
    int tallestBillboard(vector<int>& rods) {
        // dp[taller - shorter] = taller
        unordered_map<int, int> dp;
        dp[0] = 0;
        
        for (int r : rods) {
            // newDp means we don't add r to these stands.
            unordered_map<int, int> newDp(dp);
            
            for (const auto& entry : dp) {
                int diff = entry.first;
                int taller = entry.second;
                int shorter = taller - diff;
                
                // Add r to the taller stand, thus the height difference is increased to diff + r.
                int newTaller = newDp[diff + r];
                newDp[diff + r] = max(newTaller, taller + r);
                
                // Add r to the shorter stand, the height difference is expressed as abs(shorter + r - taller).
                int newDiff = abs(shorter + r - taller);
                int newTaller2 = max(shorter + r, taller);
                newDp[newDiff] = max(newTaller2, newDp[newDiff]);
            }
            
            dp = newDp;
        }
        
        // Return the maximum height with 0 difference.
        return dp[0];
    }
};
78628