We need to pick exactly k workers from a pool of n workers. Two rules pull in opposite directions. Rule 1 says every worker must be paid at least their minimum wage. Rule 2 says pay must be proportional to quality. If we fix a single "rate per unit of quality" for the group, every worker in it is paid rate * quality[i].
A high rate satisfies everyone's minimum wage but raises the total cost. A low rate fails some worker's minimum wage and disqualifies that worker. For a fixed group, the smallest rate that keeps everyone above their minimum wage is set by the most demanding worker, the one with the highest wage[i] / quality[i] ratio.
That reframes the problem. For each possible "captain" (the worker whose ratio sets the pay rate), find the cheapest set of k workers whose ratios are all at or below the captain's ratio. Pay is rate * quality[i] and the rate is fixed by the captain, so we want the k workers with the smallest total quality among those with acceptable ratios. A max-heap tracks those k smallest qualities.
1 <= k <= n <= 10^4 → With n up to 10,000, an O(n^2) scan over subsets or pairs is at the edge of acceptable and enumerating subsets is impossible. The sorting plus heap approach runs in O(n log n).1 <= quality[i], wage[i] <= 10^4 → The largest total quality is 10^4 * 10^4 = 10^8, and a ratio can be as high as 10^4, so the cost can reach roughly 10^12. That exceeds a 32-bit int, so the cost must be a double (or 64-bit) and the quality sum fits in a 32-bit int only up to 10^8, which is safe.Try every possible combination of k workers. For each subset, find the minimum valid pay rate and compute the total cost.
For any group of k workers, the pay rate must be high enough to satisfy every worker's minimum wage. Since each worker's pay is rate * quality[i], they need rate * quality[i] >= wage[i], which means rate >= wage[i] / quality[i]. The minimum valid rate for the group is the maximum of wage[i] / quality[i] across all workers in the group. Once we know the rate, the total cost is rate * (sum of all qualities in the group).
So we enumerate all C(n, k) subsets, compute the required rate and total cost for each, and return the minimum.
ratio[i] = wage[i] / quality[i] for each worker.k workers from the n available.rate * sum(quality[i]) for workers in the group.Input:
We try all C(3,2) = 3 subsets:
Enumerating every subset is impractical for n much larger than 20. The next approach avoids enumeration by fixing the rate first, then choosing the cheapest workers under that rate.
For any group of workers, the total cost factors cleanly into two parts:
cost = (max ratio in group) * (sum of qualities in group)
The "max ratio" is the wage-to-quality ratio of the most demanding worker (per unit of quality) in the group. That worker is the bottleneck that forces the pay rate up for everyone.
Sort all workers by their ratio wage[i] / quality[i] in ascending order. If worker j is the captain (the highest ratio in the group), then every worker before j in sorted order has a ratio at or below j's ratio. All of them can be hired at j's rate without violating their minimum wage, so they are eligible.
For each worker j in sorted ratio order, the eligible pool is workers 0, 1, ..., j. The rate is already fixed at ratio[j], so we pick the k workers (including j) with the smallest total quality. The cost becomes ratio[j] * (sum of the k smallest qualities among workers 0..j).
A max-heap of size k tracks the k smallest qualities as we iterate. The heap holds the k smallest qualities seen so far. When a new worker's quality is smaller than the heap's maximum, the maximum is evicted and the new value takes its place. The heap then holds the k workers with the smallest total quality among all eligible workers.
Why does iterating in sorted order cover the optimal answer? Consider any optimal group of k workers, and let c be its captain (its highest-ratio worker). When the loop reaches c, the eligible pool is exactly the workers with ratio at or below ratio[c], which includes every member of the optimal group. At that step the heap holds the k smallest qualities in the pool, so its quality sum is at most the optimal group's quality sum. The cost computed at step c, ratio[c] * (heap sum), is therefore at most the optimal cost. Since every step produces a valid group, the minimum over all steps equals the optimal cost.
wage[i] / quality[i].qualitySum).qualitySum.k, remove the largest quality from the heap and subtract it from qualitySum.k, compute cost = ratio[current] * qualitySum and update the minimum.