# Program to find final amount that should be paid to employees based on their performance in C++

Suppose we have two lists of numbers of same length called performance and costs. And we also have another number k. These indicates that each worker i performs at performance[i] level and it takes costs at least costs[i]. We have to find the minimum cost to hire k employees given also that the workers will be paid proportionate to their performance compared to other employees in the group.

So, if the input is like performance = [5, 3, 2] costs = [100, 5, 4] k = 2, then the output will be 10, as we can select emp1 and emp2. They must be paid at least 5 + 4 = 9 amount. But emp1 performs 1.5 times better than the emp2, so he should be paid at least 1.5 * 4 = 6. So in total they will get 6 + 4 = 10.

To solve this, we will follow these steps −

• n := size of c

• Define an array seq of size n

• fill seq with values 0 through n−1

• sort the array seq based on these criteria (c[i] * p[j] < c[j] * p[i])

• ans := inf, psum := 0

• define a priority queue pq

• for initialize i := 0, when i < n, update (increase i by 1), do −

• idx := seq[i]

• insert p[idx] into pq

• psum := psum + p[idx]

• if size of pq > k, then −

• psum := psum − top element of pq

• delete top element from pq

• if i >= k − 1, then −

• ans := minimum of ans and (c[idx] / p[idx] * psum)

• return ans

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
double solve(vector<int>& p, vector<int>& c, int k) {
int n = c.size();
vector<int> seq(n);
for (int i = 0; i < n; ++i)
seq[i] = i;
sort(seq.begin(), seq.end(), [&](int i, int j) { return c[i] *
p[j] < c[j] * p[i]; });
double ans = INT_MAX, psum = 0;
priority_queue<int> pq;
for (int i = 0; i < n; ++i) {
int idx = seq[i];
pq.emplace(p[idx]);
psum += p[idx];
if (pq.size() > k) {
psum −= pq.top();
pq.pop();
}
if (i >= k − 1)
ans = min(ans, (double)c[idx] / p[idx] * psum);
}
return ans;
}
int main(){
vector<int> performance = {5, 3, 2};
vector<int> costs = {100, 5, 4};
int k = 2;
cout << solve(performance, costs, k);
}

## Input

{5, 3, 2}, {100, 5, 4}, 2

## Output

10