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

The unfairness of a distribution is defined as the maximum total cookies obtained by a single child in the distribution.

Return the minimum unfairness of all distributions.

&nbsp;
Example 1:

Input: cookies = [8,15,10,20,8], k = 2
Output: 31
Explanation: One optimal distribution is [8,15,8] and [10,20]
- The 1st child receives [8,15,8] which has a total of 8 + 15 + 8 = 31 cookies.
- The 2nd 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.


Example 2:

Input: cookies = [6,1,3,2,2,4,1,2], k = 3
Output: 7
Explanation: One optimal distribution is [6,1], [3,2,2], and [4,1,2]
- The 1st child receives [6,1] which has a total of 6 + 1 = 7 cookies.
- The 2nd child receives [3,2,2] which has a total of 3 + 2 + 2 = 7 cookies.
- The 3rd 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.


&nbsp;
Constraints:


	2 &lt;= cookies.length &lt;= 8
	1 &lt;= cookies[i] &lt;= 105
	2 &lt;= k &lt;= cookies.length


class Solution:
    def distributeCookies(self, cookies: List[int], k: int) -> int:
        ans = float('inf')
        fair = [0]*k
        def rec(i):
            nonlocal ans,fair
            if i == len(cookies):
                ans = min(ans,max(fair))
                return
			# Bounding condition to stop a branch if unfairness already exceeds current optimal soltution
			if ans <= max(fair):
                return
            for j in range(k):
                fair[j] += cookies[i]
                rec(i+1)
                fair[j] -= cookies[i]
        rec(0)
        return ans
class Solution {
    int ans;
    int count[];
    public int distributeCookies(int[] cookies, int k) {
        ans= Integer.MAX_VALUE;
        count= new int[k];

        backtrack(0,cookies, k);
        return ans;
    }
    public void backtrack(int cookieNumber, int[] cookies, int k)
    {
        if(cookieNumber==cookies.length)
        {
            int max= 0;
            for(int i=0; i<k; i++)
            {
                max=Math.max(max, count[i]);

            }
            ans = Math.min(ans, max);
            return;
        }
        for(int i=0;i<k; i++)
        {
            count[i]+=cookies[cookieNumber];
            backtrack(cookieNumber+1, cookies, k);
            count[i]-=cookies[cookieNumber];
            if(count[i]==0) break;
        }
    }
}
class Solution {
public:
    int ans = INT_MAX;
    void solve(int start, vector<int>& nums, vector<int>& v, int k){
        if(start==nums.size()){
            int maxm = INT_MIN;
            for(int i=0;i<k;i++){
                maxm = max(maxm,v[i]);
            }
            ans = min(ans,maxm);
            return;
        }
        for(int i=0;i<k;i++){
            v[i] += nums[start];
            solve(start+1,nums,v,k);
            v[i] -= nums[start];
        }
    }
    
    int distributeCookies(vector<int>& nums, int k) { // nums is the cookies vector
        int n = nums.size();
        vector<int> v(k,0); // v is to store each sum of the k subsets
        solve(0,nums,v,k);
        return ans;
    }
};
var distributeCookies = function(cookies, k) {
    cookies.sort((a, b) => b - a);
    if(k === cookies.length) return cookies[0];
    
    const arr = new Array(k).fill(0);
    let res = Infinity;
    
    function helper(arr, cookies, level) {
        if(level === cookies.length) {
            const max = Math.max(...arr);
            res = Math.min(res, max);
            return;
        }
        const cookie = cookies[level];
        for(let i = 0; i < arr.length; i++) {
            arr[i] += cookie;
            helper(arr, cookies, level + 1);
            arr[i] -= cookie;
        }
    }
    
    helper(arr, cookies, 0);
    
    return res;
};
Fair Distribution of Cookies