<p>You are given an array of <code>n</code> integers, <code>nums</code>, where there are at most <code>50</code> unique values in the array. You are also given an array of <code>m</code> customer order quantities, <code>quantity</code>, where <code>quantity[i]</code> is the amount of integers the <code>i<sup>th</sup></code> customer ordered. Determine if it is possible to distribute <code>nums</code> such that:</p>

<ul>
	<li>The <code>i<sup>th</sup></code> customer gets <strong>exactly</strong> <code>quantity[i]</code> integers,</li>
	<li>The integers the <code>i<sup>th</sup></code> customer gets are <strong>all equal</strong>, and</li>
	<li>Every customer is satisfied.</li>
</ul>

<p>Return <code>true</code><em> if it is possible to distribute </em><code>nums</code><em> according to the above conditions</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4], quantity = [2]
<strong>Output:</strong> false
<strong>Explanation:</strong> The 0<sup>th</sup> customer cannot be given two different integers.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,3], quantity = [2]
<strong>Output:</strong> true
<strong>Explanation:</strong> The 0<sup>th</sup> customer is given [3,3]. The integers [1,2] are not used.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,1,2,2], quantity = [2,2]
<strong>Output:</strong> true
<strong>Explanation:</strong> The 0<sup>th</sup> customer is given [1,1], and the 1st customer is given [2,2].
</pre>

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

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
	<li><code>m == quantity.length</code></li>
	<li><code>1 &lt;= m &lt;= 10</code></li>
	<li><code>1 &lt;= quantity[i] &lt;= 10<sup>5</sup></code></li>
	<li>There are at most <code>50</code> unique values in <code>nums</code>.</li>
</ul>

class Solution:
    def canDistribute(self, nums: List[int], qs: List[int]) -> bool:
        # at most 50 uniques in nums
        # also given an array m of quantities, customer i gets qs[i]

        # can we distribute nums such that:
        #    i gets qs[i] copies of a single integer for all i

        # before: we have Counter(nums)
        # after i: decrement *one* bucket with f > qs[i] by qs[i] and proceed

        # 50 uniques, so there are 50 freqs
        # and there are at most 10 queries

        # so 2**10 for DFS + backtracking, this is feasible

        ## DFS + backtracking
        freqs = list(Counter(nums).values()) # don't care about the values, just their freqs. <= 50 elements

        N = len(qs)
        qs.sort(reverse=True)
        def possible(i: int) -> bool:
            if i == N: return True

            blocked = set()
            q = qs[i]
            for u in range(len(freqs)):
                # optimization: if decrementing a pile of size freqs[u] already failed,
                # don't try it again
                if freqs[u] >= q and freqs[u] not in blocked:
                    freqs[u] -= q
                    if possible(i+1): return True
                    # backtrack
                    freqs[u] += q
                    blocked.add(freqs[u])

            return False

        # possible problem: if we have freqs = [3,3,3,3,3,3,3...] and we just barely can't do it,
        # then we'll have tried handling the first request by decrementing all of the copies of 3, which doesn't make
        # much sense. So add a restriction: if we failed to succeed by decrementing a pile with f, don't try again

        return possible(0)
28000