<p>You are given an integer array <code>cookies</code>, where <code>cookies[i]</code> denotes the number of cookies in the <code>i<sup>th</sup></code> bag. You are also given an integer <code>k</code> that denotes the number of children to distribute <strong>all</strong> the bags of cookies to. All the cookies in the same bag must go to the same child and cannot be split up.</p>

<p>The <strong>unfairness</strong> of a distribution is defined as the <strong>maximum</strong> <strong>total</strong> cookies obtained by a single child in the distribution.</p>

<p>Return <em>the <strong>minimum</strong> unfairness of all distributions</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> cookies = [8,15,10,20,8], k = 2
<strong>Output:</strong> 31
<strong>Explanation:</strong> One optimal distribution is [8,15,8] and [10,20]
- The 1<sup>st</sup> child receives [8,15,8] which has a total of 8 + 15 + 8 = 31 cookies.
- The 2<sup>nd</sup> child receives [10,20] which has a total of 10 + 20 = 30 cookies.
The unfairness of the distribution is max(31,30) = 31.
It can be shown that there is no distribution with an unfairness less than 31.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> cookies = [6,1,3,2,2,4,1,2], k = 3
<strong>Output:</strong> 7
<strong>Explanation:</strong> One optimal distribution is [6,1], [3,2,2], and [4,1,2]
- The 1<sup>st</sup> child receives [6,1] which has a total of 6 + 1 = 7 cookies.
- The 2<sup>nd</sup> child receives [3,2,2] which has a total of 3 + 2 + 2 = 7 cookies.
- The 3<sup>rd</sup> child receives [4,1,2] which has a total of 4 + 1 + 2 = 7 cookies.
The unfairness of the distribution is max(7,7,7) = 7.
It can be shown that there is no distribution with an unfairness less than 7.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= cookies.length &lt;= 8</code></li>
	<li><code>1 &lt;= cookies[i] &lt;= 10<sup>5</sup></code></li>
	<li><code>2 &lt;= k &lt;= cookies.length</code></li>
</ul>

class Solution {
public:
    vector<vector<int>> dp;
    
    // Function to calculate the unfairness using dynamic programming
    int unfairness(vector<int>& cookies, int n, int k, int bagMask) {
        // If the subproblem has already been solved, return the pre-calculated value
        if (dp[k][bagMask] != -1)
            return dp[k][bagMask];
        
        // Function to calculate the sum of cookies in a bag based on the Mask
        auto sum_cookies = [&](int Mask) -> int {
            int sum = 0;
            for (int i = 0; i < n; i++) {
                // Check if the ith cookie is included in the bag
                if (Mask & (1 << i))
                    sum += cookies[i];
            }
            return sum;
        };

        // Base case: If there is only one bag remaining, return the sum of cookies in the bag
        if (k == 1) 
            return dp[k][bagMask] = sum_cookies(bagMask);

        int ans = INT_MAX;
        // Iterate over all possible subsets of the cookies in the bag
        for (int Mask = bagMask; Mask > 0; Mask = (Mask - 1) & bagMask) {
            // Calculate the sum of cookies in the current subset
            int sum1 = sum_cookies(Mask);
            // Recursively solve the subproblem for the remaining bags
            int sum2 = unfairness(cookies, n, k - 1, bagMask ^ Mask);
            // Update the minimum unfairness by considering the maximum of the current subset and the subproblem solution
            ans = min(ans, max(sum1, sum2));
        }

        // Store the minimum unfairness in the dp array for future reference and return it
        return dp[k][bagMask] = ans;
    }

    // Main function to distribute cookies among children while minimizing unfairness
    int distributeCookies(vector<int>& cookies, int k) {
        int n = cookies.size();
        // Initialize the dp array with -1 to indicate that subproblems have not been solved yet
        dp.assign(k + 1, vector<int>(1 << n, -1));
        // Start the recursive dynamic programming solution from the full bag mask
        return unfairness(cookies, n, k, (1 << n) - 1);
    }
};

9800