<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>

#include <bits/stdc++.h>
#define ss second
#define ff first
#define pii pair<int,int>
#define pb push_back
#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>    
#define pll pair<ll,ll>
#define vpll vector<pll>
#define vvll vector<vll>
#define ld long double
#define um unordered_map
#define all(x) x.begin(),x.end()

using namespace std;

class Solution {
public:
    bool canFinish(vector<int>& jobs, vector<ll>& workers, int idx, ll maxWorkload) {
        if (idx < 0) return true;
        
        unordered_set<ll> seen; // seen is an unordered set that keeps track of the workloads that have already been attempted for the current job.(uncanny place to add memoization)
        for (int i = 0; i < workers.size(); i++) {
            if (seen.count(workers[i])) continue; // Skip duplicate workload states
            if (workers[i] + jobs[idx] > maxWorkload) continue; // Prune branch if workload exceeds maxWorkload
            
            seen.insert(workers[i]);
            workers[i] += jobs[idx];
            if (canFinish(jobs, workers, idx - 1, maxWorkload)) {
                return true;
            }
            workers[i] -= jobs[idx];
        }
        
        return false;
    }
    
    int minimumTimeRequired(vector<int>& jobs, int k) {
        sort(all(jobs), greater<int>()); // Sort jobs in decreasing order for better pruning
        ll left = *max_element(all(jobs)); // Minimum possible maximum workload
        ll right = accumulate(all(jobs), 0LL); // Maximum possible maximum workload

        while (left < right) {
            ll mid = left + (right - left) / 2;
            vector<ll> workers(k, 0);
            if (canFinish(jobs, workers, jobs.size() - 1, mid)) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        
        return left;
    }
};

50836