<p>You are given an integer array <code>jobs</code>, where <code>jobs[i]</code> is the amount of time it takes to complete the <code>i<sup>th</sup></code> job.</p>

<p>There are <code>k</code> workers that you can assign jobs to. Each job should be assigned to <strong>exactly</strong> one worker. The <strong>working time</strong> of a worker is the sum of the time it takes to complete all jobs assigned to them. Your goal is to devise an optimal assignment such that the <strong>maximum working time</strong> of any worker is <strong>minimized</strong>.</p>

<p><em>Return the <strong>minimum</strong> possible <strong>maximum working time</strong> of any assignment. </em></p>

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

<pre>
<strong>Input:</strong> jobs = [3,2,3], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> By assigning each person one job, the maximum time is 3.
</pre>

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

<pre>
<strong>Input:</strong> jobs = [1,2,4,7,8], k = 2
<strong>Output:</strong> 11
<strong>Explanation:</strong> Assign the jobs the following way:
Worker 1: 1, 2, 8 (working time = 1 + 2 + 8 = 11)
Worker 2: 4, 7 (working time = 4 + 7 = 11)
The maximum working time is 11.</pre>

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

<ul>
	<li><code>1 &lt;= k &lt;= jobs.length &lt;= 12</code></li>
	<li><code>1 &lt;= jobs[i] &lt;= 10<sup>7</sup></code></li>
</ul>

class Solution:
    def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
        n = len(jobs)
        l, r = max(jobs), sum(jobs)
        jobs.sort(reverse=True)

        def check(target, bitmask, worker, start, time, memo):
            if time > target:
                return False
            if worker == k:
                return bitmask == 2**n - 1

            state = (bitmask, worker, time)
            if state in memo:
                return memo[state]

            if time == target:
                if check(target, bitmask, worker + 1, 0, 0, memo):
                    memo[state] = True
                else:
                    memo[state] = False
                return memo[state]

            last = -1
            for i in range(start, n):
                if bitmask & (1 << i) or jobs[i] == last:
                    continue
                bitmask |= 1 << i
                if check(target, bitmask, worker, i + 1, time + jobs[i], memo):
                    memo[state] = True
                    return memo[state]
                bitmask ^= 1 << i
                last = jobs[i]
                if time == 0:
                    return False
            if check(target, bitmask, worker + 1, 0, 0, memo):
                memo[state] = True
            else:
                memo[state] = False
            return memo[state]

        while l <= r:
            mid = (l + r) // 2
            if check(mid, 0, 0, 0, 0, {}):
                r = mid - 1
            else:
                l = mid + 1
        return l


class Solution:
    def minimumTimeRequired(self, jobs: List[int], k: int) -> int:
        n = len(jobs)
        jobs.sort(reverse=True)
        workers = [0] * k
        res = sum(jobs)

        def f(curr):
            nonlocal res
            if curr == n:
                res = min(res, max(workers))
                return

            last = -1
            for i in range(k):
                if workers[i] + jobs[curr] != last and workers[i] + jobs[curr] < res:
                    workers[i] += jobs[curr]
                    f(curr + 1)
                    last = workers[i]
                    workers[i] -= jobs[curr]
                    if workers[i] == 0:
                        break
            return

        f(0)
        return res

3061