<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:
        workloads = [0] * k
        self.res = float('inf')
        self.backtrack(jobs, 0, workloads)
        return self.res

    # 记录所有分配方案中的「最大工作时间」的最小值
    res = float('inf')

    def backtrack(self, jobs: List[int], jobIdx: int, workloads: List[int]) -> None:
        if jobIdx == len(jobs):
            # 找到一个分配方案
            # 计算当前分配方案下的最大工作时间
            max_workload = max(workloads)
            # 更新「所有分配方案中的最大工作时间」的最小值
            self.res = min(self.res, max_workload)
            return

        # 从当前的 jobs[jobIdx] 来选择工人
        # 注意，这个 chosen 是一个关键的剪枝优化，具体看我在思路部分的解释
        chosen = set()
        for workerIdx in range(len(workloads)):
            if workloads[workerIdx] + jobs[jobIdx] >= self.res:
                # 剪枝优化：如果当前工人的工作时间加上当前的工作时间已经超过了当前的最优解，那么就不用继续尝试了
                continue
            if workloads[workerIdx] in chosen:
                # 剪枝优化：如果前面曾有工人有这个 workload，则不必把当前工作分配给他
                continue

            # 做选择，将 jobs[jobIdx] 分配给工人 workreIdx
            chosen.add(workloads[workerIdx])
            workloads[workerIdx] += jobs[jobIdx]
            self.backtrack(jobs, jobIdx + 1, workloads)
            # 撤销选择
            workloads[workerIdx] -= jobs[jobIdx]
1916