


1) Every candidate in the paid group should be paid in the ratio of their skill compared to other candidates in the paid group.
2) The minimum salary expectation of every candidate in the paid group should be fulfilled.
Answers which are within the range 10^-6 of the correct answer will be considered correct.
The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains an integer ‘N’ representing the number of total candidates.
The second line of each test case contains ‘N’ space-separated integers representing the elements of the ‘SKILL’ array.
The third line of each test case contains ‘N’ space-separated integers representing the elements of ‘EXPECTED_SALARY’ array.
The last line of each test case contains an integer ‘M’ representing the number of candidates to be hired.
For each test case, return the minimum cost to hire ‘M’ candidates.
The output of each test case will be printed in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= M <= N <= 10^3
1 <= SKILL[ i ] <= 10^3
1 <= EXPECTED_SALARY[ i ] <= 10^3
Time limit: 1 second
We have two rules to follow:
Where 1 <= i, j <= N.
To minimise the total cost, at least one candidate will be paid exactly equals to his/her minimum salary expectation. Otherwise, we will be paying everyone more than their expectation resulting in an increase in total cost, which is not desirable.
Now as per Rule 1, we can scale the salary of all other candidates if we fix the salary of one candidate. Let us say that the candidate who is paid exactly equals his/her minimum salary is termed as ‘REPRESENTATIVE’. So, Rule 1 is settled.
As per Rule 2,
SKILL[i] : SKILL[REPRESENTATIVE] = SALARY[i] : SALARY[REPRESENTATIVE]
or, SKILL[i] : SKILL[REPRESENTATIVE] = SALARY[i] : EXPECTED_SALARY[REPRESENTATIVE]
or, (SKILL[i] : SKILL[REPRESENTATIVE]) * EXPECTED_SALARY[REPRESENTATIVE] = SALARY[i]
or, SALARY[i] = SKILL[i] * (EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE])
or, SALARY[i] = SKILL[i] * REPRESENTATIVE_RATIO
Where REPRESENTATIVE_RATIO = (EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE]).
For every ‘REPRESENTATIVE’, we will find the salaries of all other candidates using the formula mentioned above. After finding the salary of every candidate, we will choose the lowest ‘M - 1’ salary candidates among ‘N - 1’ candidates. And store the total cost (including salary of the representative candidate) in a variable, ‘COST’.
We will repeat the above procedure for every candidate considering them as ‘REPRESENTATIVE’ and store the minimum of all ‘COST’ in ‘MIN_COST’.
Algorithm:
As mentioned in Approach 1, there must be at least one candidate, ‘REPRESENTATIVE’ who is paid exactly equals to his/her expected salary, otherwise, it will not be optimum. The ‘REPRESENTATIVE’ will allow us to easily choose the rest of the ‘M - 1’ candidates.
After fixing the ‘REPRESENTATIVE’, any candidate ‘i’ would be paid:
SALARY[i] = (EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE) * SKILL[i]
From Rule 2,
SALARY[i] >= EXPECTED_SALARY[i]
or, (EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE]) * SKILL[i] >= EXPECTED_SALARY[i]
or, EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE] >= EXPECTED_SALARY[i] / SKILL[i]
This means that the ratio of “EXPECTED_SALARY / SKILL” for the ‘REPRESENTATIVE’ must be the highest in the group. So, in a group of ‘M’ candidates, the candidate with the highest “EXPECTED_SALARY / SKILL” ratio will be ‘REPRESENTATIVE’.
Also, the total cost of hiring ‘M’ candidates is
COST = (EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE]) * (SKILL[0] + SKILL[1] + . . . + SKILL[M-1])
or, COST = (EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE]) * (total sum of skills of ‘M’ candidates)
Let us say, EXPECTED_SALARY[REPRESENTATIVE]) / SKILL[REPRESENTATIVE] = REPRESENTATIVE_RATIO.
So to minimize the total cost, we want a small ratio. For every ratio, we find the minimum possible total skill of ‘M’ candidates.
In this way, we will repeat the process and we can find the minimum total cost.
Algorithm: