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

struct Hash {
    size_t operator()(const vector<int>& v) const {
        return v[0] ^ v[1];
    }
};

class Solution {
public:
    unordered_map<vector<int>, int, Hash> dp;
    int threshold = 0;

    int getSum(int mask, vector<int>& jobs) {
        int ans = 0;
        int idx = 0;

        while(mask != 0) {
            if ((mask & 1) == 1) {
                ans += jobs[idx];
            }

            idx++;
            mask = mask >> 1;
        }

        return ans;
    }

    int getMaskSum(int mask) {
        int ans = 0;
        while(mask != 0) {
            ans += (mask & 1);
            mask = mask >> 1;
        }
        return ans;
    }

    int solve(int k, int mask, vector<int>& jobs) {
        
        if (dp.find({k, mask}) != dp.end()) return dp[{k, mask}];
        
        if (k == 1) {
            dp[{k, mask}] = getSum(mask, jobs);
            return dp[{k, mask}];
        }

        int ans = INT_MAX;

        for (int i = mask; i != 0; i = mask&(i-1)) {
            int cur_sum = getSum(i, jobs);
            if (cur_sum > threshold) continue;
            int remaining_jobs = getMaskSum(mask ^ i);
            if (remaining_jobs < k-1) continue;
            int rest_sum = solve(k-1, mask ^ i, jobs);
            int cur_ans = max(cur_sum, rest_sum);
            ans = min(ans, cur_ans);
        }

        dp[{k, mask}] = ans;

        return ans;
    }

    int minimumTimeRequired(vector<int>& jobs, int k) {
        int n = 1 << jobs.size();
        n--;

        sort(jobs.begin(), jobs.end(), std::greater<int>());

        vector<int> q(k, 0);

        int curr_w = 0;

        for (int a = 0; a < jobs.size(); a++) {
            q[curr_w] += jobs[a];
            threshold = max(threshold, q[curr_w]);
            curr_w++;
            curr_w %= k;
        }

        return solve(k, n, jobs);
    }
};
139644