Last Updated: April 2, 2026
We have some bags of cookies and some children. Every bag must be given to exactly one child (no splitting bags), and we want to minimize the maximum total any single child receives. In other words, we want the most "fair" distribution, where fairness is measured by how heavy the most-loaded child's pile is.
This is fundamentally an assignment problem. Each bag must go to one of k children, and we want the assignment that minimizes the maximum sum across all children. Think of it like partitioning the array into k groups to minimize the maximum group sum.
The key observation is that cookies.length is at most 8. That's a tiny search space. Each of the n bags can be assigned to any of the k children, giving us k^n total assignments. With k and n both at most 8, the worst case is 8^8 = about 16.7 million, which is manageable with backtracking and pruning.
2 <= cookies.length <= 8 -> With at most 8 bags, exponential approaches like backtracking through all assignments (k^n) are perfectly feasible. Even 8^8 ~ 16.7 million is well within time limits.1 <= cookies[i] <= 10^5 -> Cookie counts are positive, so every child who gets at least one bag will have a positive total. The maximum possible total for one child is 8 * 10^5 = 800,000.2 <= k <= cookies.length -> There are at least as many bags as children, so it's always possible to give each child at least one bag.The most natural approach: for each bag of cookies, try assigning it to each of the k children. Once all bags are assigned, compute the maximum total across all children and track the minimum.
Since each of the n bags can go to any of k children, there are k^n total assignments. With n = 8 and k = 8 in the worst case, that's 8^8 = 16,777,216 assignments. That's a lot, but it's within the range of what a computer can handle in a couple of seconds.
We use a simple recursive approach: process bags one at a time, and for each bag, try giving it to each child. When we've assigned all bags, we record the unfairness (the max of all children's totals) if it's better than what we've seen so far.
children of size k, initialized to zeros, to track each child's current cookie total.cookies[i] to that child's total.children and update the global minimum if it's lower.Input:
We try every possible assignment of 5 bags to 2 children. For example, giving all bags to Child 0 yields max = 61. Giving [8,15,8] to Child 0 and [10,20] to Child 1 yields max(31, 30) = 31. Among all k^n = 32 assignments, the minimum unfairness is 31.
The brute force tries every assignment, but many are redundant. If multiple children have the same total, assigning a bag to any of them is equivalent. What if we skipped redundant assignments and pruned hopeless branches early?
The brute force works but does a lot of unnecessary work. Two key observations let us prune the search tree dramatically:
There's a third useful optimization: sorting the cookies array in descending order. By assigning the largest bags first, we reach higher totals sooner, which triggers the upper bound pruning earlier and cuts off more branches.
The pruning optimizations don't change the correctness of the algorithm. They only skip branches that are guaranteed to not improve the result. Upper bound pruning is safe because adding more cookies to a child can only increase their total, never decrease it. Symmetry breaking is safe because if two children have the same total, assigning the current bag to either one produces structurally identical subproblems.
cookies in descending order (largest bags first for earlier pruning).children of size k, initialized to zeros.result initialized to the total sum of all cookies.The backtracking with pruning is fast enough for these constraints, but there's a fundamentally different approach using bitmask DP that gives a cleaner complexity guarantee.
Instead of assigning bags to children one at a time, we represent which bags have been assigned using a bitmask. If there are n bags, a bitmask of n bits can represent any subset. Bit i being set means bag i has been assigned.
First, precompute the sum of cookies for every possible subset (all 2^n subsets). Then define dp[j][mask] as the minimum unfairness when distributing the bags in mask among the first j children. For each child, we try every possible subset of the remaining bags to give them, and take the minimum over all valid partitions.
This approach has a clean O(k * 3^n) complexity because for each mask, iterating over all subsets of that mask across all masks totals 3^n (by the inclusion-exclusion principle: each element is in the outer mask, in the inner subset, or in neither).
The DP formulation correctly considers every way to partition the bags among k children. By iterating over subsets of subsets, we cover all possible groupings. The max in the transition ensures we track the child with the heaviest load (the unfairness), and the min over all choices ensures we pick the partition that minimizes that maximum.
subsetSum[mask] for all 2^n masks, where each mask represents a subset of bags.dp[0][0] = 0 (zero children assigned, no bags distributed, unfairness is 0) and everything else to infinity.sub of mask and compute dp[j][mask] = min(dp[j][mask], max(dp[j-1][mask ^ sub], subsetSum[sub])).dp[k][(1 << n) - 1] (all k children assigned, all bags distributed).