Ample Syrup (Large)
Time limit5sMemory limit512 MB
Choose K of N cylindrical pancakes and stack them largest radius first to maximize exposed surface area, reported as a multiple of pi.
- Level
Medium6 of 10
- Topics
- Greedy, Sorting, Prefix sum, Brute force
- Solved
- No attempts yet
Problem
The kitchen at the Infinite House of Pancakes just received an order for a stack of pancakes. The chef has pancakes available, where . Each pancake is a cylinder, and different pancakes may have different radii and heights.
As the sous-chef, you pick of the available pancakes, discard the others, and arrange those pancakes in a stack on a plate as follows. First, take the picked pancake with the largest radius and lay it on the plate on one of its circular faces. If several pancakes share the largest radius, use any one of them. Then take the remaining pancake with the next largest radius, lay it on top, and continue until all pancakes are stacked. The centers of the circular faces all lie on one line perpendicular to the plate, as in this picture:

There is one thing your diners love as much as they love pancakes: syrup. More exposed pancake surface means more room for syrup, so the total exposed surface area should be as large as possible. Any part of a pancake that touches neither another pancake nor the plate counts as exposed.
If you pick the pancakes optimally, what is the largest total exposed pancake surface area?
Input
The first line contains the number of test cases, . test cases follow. Each begins with one line holding two integers and : the number of available pancakes, and the size of the stack the diner ordered. Then lines follow. Each holds two integers and : the radius and the height of the -th pancake, in millimeters.
Limits
Output
The largest total exposed surface area is always an exact integer multiple of . For each test case, print one line holding Case #x: y, where is the test case number starting from 1 and is the integer with the property that the largest total exposed surface area equals square millimeters. Print the integer , not the area as a decimal.
Explanation
In the first test case the stack holds a single pancake. A stack of just the first pancake exposes mm, and a stack of just the second pancake exposes mm. The second pancake is better, so the answer is 44000.
In the second test case both pancakes from the first case go into the stack. The first pancake contributes its whole top face and its side, mm. The second pancake contributes the part of its top face that stays uncovered plus its side, mm. Together that is mm, so the answer is 48000.
In the third test case every pancake has radius 100 and height 10. Stacking two of them gives a single cylinder of radius 100 and height 20, which exposes mm, so the answer is 14000.
In the fourth test case the best stack uses the pancakes with radii 8 and 9. Its area is mm, so the answer is 199.