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

using ll = int;

class Solution {
public:
    int minimumTimeRequired(vector<int>& a, int k) {
        ll n = a.size(), l = 0, r = 1000000000, z = (1LL<<n);
        vector<vector<ll>> f;
        for (int i=0;i<z;i++)
            f.push_back({__builtin_popcount(i), i});
        sort(f.begin(), f.end());
        function<bool(ll)> possible = [&](ll m)->bool{
            vector<pair<ll,ll>> dp(z, {k+1, -1});
            dp[0] = {1, 0};
            for (int o=0;o<z;o++)
                for (int i=0;i<n;i++)
                {
                    int j = f[o][1];
                    if (a[i]>m) return false;
                    pair<ll,ll> p = {dp[j].first + 1, (ll)a[i]};
                    if (dp[j].second + a[i] <= m)
                        p = {dp[j].first, dp[j].second + a[i]};
                    ll meow = j | (1LL<<i);
                    dp[meow] = dp[meow] < p ? dp[meow] : p;
                }
            return dp[z-1].first<=k;
        };
        while(l+1<r){
            ll m = (l+r)/2;
            if (possible(m)) r = m;
            else l = m;
        }
        return r;
    }
};
36035