<p>You have <code>n</code> jobs and <code>m</code> workers. You are given three arrays: <code>difficulty</code>, <code>profit</code>, and <code>worker</code> where:</p>

<ul>
	<li><code>difficulty[i]</code> and <code>profit[i]</code> are the difficulty and the profit of the <code>i<sup>th</sup></code> job, and</li>
	<li><code>worker[j]</code> is the ability of <code>j<sup>th</sup></code> worker (i.e., the <code>j<sup>th</sup></code> worker can only complete a job with difficulty at most <code>worker[j]</code>).</li>
</ul>

<p>Every worker can be assigned <strong>at most one job</strong>, but one job can be <strong>completed multiple times</strong>.</p>

<ul>
	<li>For example, if three workers attempt the same job that pays <code>$1</code>, then the total profit will be <code>$3</code>. If a worker cannot complete any job, their profit is <code>$0</code>.</li>
</ul>

<p>Return the maximum profit we can achieve after assigning the workers to the jobs.</p>

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

<pre>
<strong>Input:</strong> difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
<strong>Output:</strong> 100
<strong>Explanation:</strong> Workers are assigned jobs of difficulty [4,4,6,6] and they get a profit of [20,20,30,30] separately.
</pre>

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

<pre>
<strong>Input:</strong> difficulty = [85,47,57], profit = [24,66,99], worker = [40,25,25]
<strong>Output:</strong> 0
</pre>

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

<ul>
	<li><code>n == difficulty.length</code></li>
	<li><code>n == profit.length</code></li>
	<li><code>m == worker.length</code></li>
	<li><code>1 &lt;= n, m &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= difficulty[i], profit[i], worker[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
#if 1
        vector<vector<int>> jobs;
        for (int i = 0; i < difficulty.size(); i++) {
            jobs.push_back(vector<int> {difficulty[i], profit[i]});
        }
        sort(jobs.begin(), jobs.end(), [](const vector<int>& a, const vector<int>& b) {
            return a[0] < b[0];
        });
        vector<int> profitMax(profit.size());
        int maxSofar = profitMax[0] = jobs[0][1];
        
        for (int i = 1; i < profit.size(); i++) {
            maxSofar = max(maxSofar, jobs[i][1]);
            profitMax[i] = maxSofar;
        }

        for (int i = 0; i < jobs.size(); i++) {
            cout << "(" << jobs[i][0] << "," << jobs[i][1] << "),";
        }
        cout << "\n";
        for (int i = 0; i < jobs.size(); i++) {
            cout << profitMax[i] << ",";
        }
        cout << "\n";


        int total = 0;
        for (auto w : worker) {
#if 1
            auto upper = upper_bound(jobs.begin(), jobs.end(), vector<int> {w, 0}, [](const vector<int>& a, const vector<int>& b) {
                return a[0] < b[0];
        });
            if (upper == jobs.end()) {
                total += profitMax.back();
            } else if (upper != jobs.begin()) {
                total += profitMax[distance(jobs.begin(), prev(upper))];
            }
#else
            auto lower = lower_bound(jobs.begin(), jobs.end(), vector<int> {w, 0}, [](const vector<int>& a, const vector<int>& b) {
                return a[0] < b[0];
        });
            if (lower == jobs.end()) {
                cout << "back: " << profitMax.back() << "\n";
                total += profitMax.back();
            } else if ((*lower)[0] == w) {
                total += profitMax[distance(jobs.begin(), lower)];
                cout << "m: " << profitMax[distance(jobs.begin(), lower)] << "\n";
            } else {
                if (lower != jobs.begin()) {
                    total += profitMax[distance(jobs.begin(), prev(lower))];
                    cout << "p: " << profitMax[distance(jobs.begin(), prev(lower))] << "\n";
                }
            }
#endif
        }
        return total;
#else
        vector<vector<int>> jobs;
        for (int i = 0; i < difficulty.size(); i++) {
            jobs.push_back(vector<int> {difficulty[i], profit[i]});
        }
        sort(jobs.begin(), jobs.end(), [](const vector<int>& a, const vector<int>& b) {
            return a[0] < b[0];
        });
        int total = 0;
        for (auto w : worker) {
            auto lower = lower_bound(jobs.begin(), jobs.end(), vector<int> {w, 0}, [](const vector<int>& a, const vector<int>& b) {
                return a[0] < b[0];
        });
            if (lower == jobs.end()) {
                total += jobs.back()[1];
            } else if ((*lower)[0] == w) {
                total += (*lower)[1];
            } else {
                if (lower != jobs.begin()) {
                    total += (*prev(lower))[1];
                }
            }
        }
        return total;
#endif
    }
};
53546