Ninja is forming a team of at most ‘K’ engineers out of the ‘N’ engineers. For each engineer, Ninja has his ‘SPEED’ and his ‘EFFICIENCY’. He wants to form the team so that the ‘performance’ of the team is maximal.
The ‘performance’ of the team is defined as the sum of the ‘SPEED’ of the engineers present in the team multiplied by the minimum ‘EFFICIENCY’ among the engineers in the same team. Since Ninja is busy with other work, he wants your help to solve this task.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains two positive integers, ‘N’, and ‘K’, denoting the total number of engineers and the maximum number of engineers in a team.
The second line of each test case contains ‘N’ space-separated integers, ‘SPEED[i]’, representing the ith engineer’s speed.
The third line of each test case contains ‘N’ space-separated integers, ‘EFFICIENCY[i]’, representing the ith engineer’s efficiency.
Output Format:
For each test case, print the ‘maximum performance’ of the team modulo ‘10^9 + 7’, as described in the problem statement.
Output for each test case will be printed in a separate line.
Note:
1. You do not need to print anything. It has already been taken care of. Just implement the given function.
2. Since the answer can be a big number, you have to return its modulo ‘10^9 + 7’.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^4
1 <= ‘K’ <= ‘N’
1 <= ‘SPEED[i]’, ‘EFFICIENCY[i]’ <= 10^4
SPEED.length == EFFICIENCY.length == N
Where ‘SPEED[i]’ is the speed of the i-th engineer, and ‘EFFICIENCY[i]’ is the efficiency of the i-th engineer.
Time Limit: 1 sec
1
5 2
3 2 4 3 1
5 2 4 3 2
28
We get the maximum performance of the team by selecting the following engineers:
1. 1st engineer, speed = 3, efficiency = 5.
2. 3rd engineer, speed = 4, efficiency = 4.
So, the performance of the team = (3 + 4) * min(5,4) = 28. There is no other way to get a performance greater than 28.
1
5 2
3 2 4 1 2
5 1 2 3 2
15
We get the team’s maximum performance by selecting only one engineer, i.e., the first engineer, speed = 3, and efficiency = 5. So, the performance become = 3 * 5 = 15.
Notice that we can also form a team with the two engineers, but it is not possible to get the performance greater than 15 in any other way.
Think of sorting based on efficiency.
O(N * logN), where ‘N’ is the number of engineers.
We are storing the engineers based on their ‘EFFICIENCY’, which will take ‘O(N*logN)’. To perform the ‘min-heap’ operations, it will take O(logK) because the heap’s maximum size can go up to ‘K’, and we are performing these operations ‘N’ times. So, to deal with the heap we require ‘O(N*logK)’ time. Hence, the overall time complexity will be O(N*logN + N*logK) which is nothing but the O(N * logN).
O(N), where ‘N’ is the number of engineers.
As we are using an array to store the engineer’s data, that is of the size ‘N’. Hence, the space complexity will be O(N).