

‘N = 3’, ‘EXTRA = 2’
‘BULBS = [ [2, 3], [3, 5], [2, 2] ]
To get the maximum average working ratio, assign one bulb to shop ‘0’ and one bulb to shop ‘1’. So, the average working ratio will be equal to ‘(3/4 + 4/6 + 2/2) / 3 = 0.80556’.
1. The answers within ‘10^(-5)’ of the actual answer will be accepted.
2. You do not need to print anything, it has already been taken care of. Just implement the function.
The first line of input contains an integer ‘T’ which denotes the number of test cases. Then, the ‘T’ test cases follow.
Each test case’s first line contains two integers, ‘N’ and ‘EXTRA’, denoting the number of shops and extra bulbs. Then, ‘n’ lines follow.
Each line contains two integers, ‘WORKS’ and ‘TOTAL’, denoting an element in the array ‘BULBS’.
For every test case, return the maximum possible average working ratio. The printed output will be up to ‘5’ decimal places.
The output of each test case will be printed in a separate line.
1 <= T <= 10
1 <= N, EXTRA <= 10^3
1 <= WORKS <= TOTAL <= 10^5
Each element of ‘BULBS’ contains exactly two integers.
Where ‘T’ is the number of test cases, ‘N’ is the number of shops, ‘EXTRA’ is the number of extra bulbs, and ‘[WORKS, TOTAL]’ represents an element in the ‘BULBS’ array.
Time limit: 1 second
Let ‘PROFIT’ be the difference between the new and previous working ratio. The new working ratio is obtained by adding some extra bulbs to the previous. Observe that the value of ‘PROFIT’ keeps decreasing when you add extra bulbs.
At any instance, the shop which gives the maximum ‘PROFIT’ by taking one extra bulb should be assigned that extra bulb so as to get the maximum sum of working ratios. In each iteration, we search for the shop with maximum ‘PROFIT’, assign one extra bulb to it, and update the ‘PROFIT’ and ‘BULBS’ values for the assigned shop.
Algorithm:
We optimize the previous approach by using a priority queue (in this case, a max-heap) to find the shop that gives the maximum ‘PROFIT’ by taking one extra bulb. In each iteration, we pop the top of the priority queue, which is the shop with maximum ‘PROFIT’, update its ‘PROFIT’ and ‘BULBS’ values, and insert it into the priority queue with the new ‘PROFIT’ value.
Algorithm: