Ample Syrup (Large)

Time limit5sMemory limit512 MB

Summary
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 KK pancakes. The chef has NN pancakes available, where N≥KN \ge K. Each pancake is a cylinder, and different pancakes may have different radii and heights.

As the sous-chef, you pick KK of the NN available pancakes, discard the others, and arrange those KK 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 KK pancakes are stacked. The centers of the circular faces all lie on one line perpendicular to the plate, as in this picture:

A stack of pancakes

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 KK pancakes optimally, what is the largest total exposed pancake surface area?

Input

The first line contains the number of test cases, TT. TT test cases follow. Each begins with one line holding two integers NN and KK: the number of available pancakes, and the size of the stack the diner ordered. Then NN lines follow. Each holds two integers RiR_i and HiH_i: the radius and the height of the ii-th pancake, in millimeters.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤K≤N≤10001 \le K \le N \le 1000
  • 1≤Ri≤1061 \le R_i \le 10^6
  • 1≤Hi≤1061 \le H_i \le 10^6

Output

The largest total exposed surface area is always an exact integer multiple of π\pi. For each test case, print one line holding Case #x: y, where xx is the test case number starting from 1 and yy is the integer with the property that the largest total exposed surface area equals yπy\pi square millimeters. Print the integer yy, 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 πR02+2πR0H0=14000π\pi R_0^2 + 2 \pi R_0 H_0 = 14000\pi mm2^2, and a stack of just the second pancake exposes 44000π44000\pi mm2^2. 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, 14000π14000\pi mm2^2. The second pancake contributes the part of its top face that stays uncovered plus its side, 34000π34000\pi mm2^2. Together that is 48000π48000\pi mm2^2, 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 14000π14000\pi mm2^2, so the answer is 14000.

In the fourth test case the best stack uses the pancakes with radii 8 and 9. Its area is 199π199\pi mm2^2, so the answer is 199.

Examples1

  1. Example 1

    Input
    4
    2 1
    100 20
    200 10
    2 2
    100 20
    200 10
    3 2
    100 10
    100 10
    100 10
    4 2
    9 3
    7 1
    10 1
    8 4
    
    Expected output
    Case #1: 44000
    Case #2: 48000
    Case #3: 14000
    Case #4: 199