<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>

// 3d -> MLE -> The state should be the difference of the 2 sums not the 2 sums individually

// class Solution {
// private:
//     int support(vector<int>& nums, int sum1, int sum2, int index, vector<vector<vector<int>>>& memo) {
//         if(index >= nums.size()) {
//             return sum1==sum2? 0: INT_MIN;
//         }
//         if(memo[sum1][sum2][index] != -1) {
//             return memo[sum1][sum2][index];
//         }
//         int add1 = support(nums, sum1+nums[index], sum2, index+1, memo);
//         int add2 = nums[index]+support(nums, sum1, sum2+nums[index], index+1, memo);
//         int skip = support(nums, sum1, sum2, index+1, memo);
//         return memo[sum1][sum2][index] = max({add1, add2, skip});
//     }
// public:
//     int tallestBillboard(vector<int>& rods) {
//         int sum = 0;
//         for(int ele: rods) {
//             sum += ele;
//         }
//         vector<vector<vector<int>>> memo(sum+1, vector<vector<int>> (sum+1, vector<int> (rods.size()+1, -1)));
//         return support(rods, 0, 0, 0, memo);
//     }
// };


class Solution {
private:
    int support(vector<int>& nums, int diff, int index, vector<vector<int>>& memo, int& sum) {
        if(index >= nums.size()) {
            return diff == 0? 0: INT_MIN;
        }
        if(memo[diff+sum][index] != -1) {
            return memo[diff+sum][index];
        }
        int add1 = support(nums, diff+nums[index], index+1, memo, sum);
        int add2 = nums[index] + support(nums, diff-nums[index], index+1, memo, sum);
        int skip = support(nums, diff, index+1, memo, sum);
        return memo[diff+sum][index] = max({add1, add2, skip});
    }
public:
    int tallestBillboard(vector<int>& rods) {
        int sum = 0;
        for(int ele: rods) {
            sum += ele;
        }
        vector<vector<int>> memo(2*sum+1, vector<int> (rods.size()+1, -1));
        return support(rods, 0, 0, memo, sum);
    }
};
75909