<p>There are <code>n</code> workers. You are given two integer arrays <code>quality</code> and <code>wage</code> where <code>quality[i]</code> is the quality of the <code>i<sup>th</sup></code> worker and <code>wage[i]</code> is the minimum wage expectation for the <code>i<sup>th</sup></code> worker.</p>

<p>We want to hire exactly <code>k</code> workers to form a <strong>paid group</strong>. To hire a group of <code>k</code> workers, we must pay them according to the following rules:</p>

<ol>
	<li>Every worker in the paid group must be paid at least their minimum wage expectation.</li>
	<li>In the group, each worker&#39;s pay must be directly proportional to their quality. This means if a worker&rsquo;s quality is double that of another worker in the group, then they must be paid twice as much as the other worker.</li>
</ol>

<p>Given the integer <code>k</code>, return <em>the least amount of money needed to form a paid group satisfying the above conditions</em>. Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>

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

<pre>
<strong>Input:</strong> quality = [10,20,5], wage = [70,50,30], k = 2
<strong>Output:</strong> 105.00000
<strong>Explanation:</strong> We pay 70 to 0<sup>th</sup> worker and 35 to 2<sup>nd</sup> worker.
</pre>

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

<pre>
<strong>Input:</strong> quality = [3,1,10,10,1], wage = [4,8,2,2,7], k = 3
<strong>Output:</strong> 30.66667
<strong>Explanation:</strong> We pay 4 to 0<sup>th</sup> worker, 13.33333 to 2<sup>nd</sup> and 3<sup>rd</sup> workers separately.
</pre>

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

<ul>
	<li><code>n == quality.length == wage.length</code></li>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= quality[i], wage[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
private:
    struct Worker {
        double ratio;  // wage to quality ratio
        int quality;
    };

    // Comparator struct for sorting Workers by their wage-to-quality ratio
    struct CompareWorkers {
        // Overload the () operator to compare two Worker objects
        bool operator()(const Worker& a, const Worker& b) const {
            return a.ratio < b.ratio; // Sort by ratio in ascending order
        }
    };

public:
    double mincostToHireWorkers(std::vector<int>& quality, std::vector<int>& wage, int K) {
        int n = quality.size();
        std::vector<Worker> workers(n);

        // Step 1: Calculate the wage-to-quality ratio for each worker and store it in the workers vector
        for (int i = 0; i < n; ++i) {
            workers[i] = {static_cast<double>(wage[i]) / quality[i], quality[i]};
        }

        // Step 2: Sort workers by their wage-to-quality ratio
        std::sort(workers.begin(), workers.end(), CompareWorkers());

        // Step 3: Use a max-heap to maintain the K workers with the smallest total quality
        std::priority_queue<int> maxHeap;
        int totalQuality = 0;
        double minCost = 1e9;

        // Step 4: Iterate through the sorted workers and calculate the minimum cost
        for (const auto& worker : workers) {
            totalQuality += worker.quality;
            maxHeap.push(worker.quality);

            // If we have more than K workers, remove the worker with the largest quality
            if (maxHeap.size() > K) {
                totalQuality -= maxHeap.top();
                maxHeap.pop();
            }

            // If we have exactly K workers, calculate the cost
            if (maxHeap.size() == K) {
                minCost = std::min(minCost, totalQuality * worker.ratio);
            }
        }

        return minCost;
    }
};

// Time Complexity: The time complexity is O(n * log n), where n is the number of workers. This is because:
// • Sorting the workers takes O(n * log n).
// • Each insertion and removal operation in the heap takes O(log K), and we do this for all n workers.

// Space Complexity: The space complexity is O(n) due to the storage required for the workers vector and the max-heap.
28100