#P10920. [2015杭电多校]Delicious Apples
[2015杭电多校]Delicious Apples
Delicious Apples
Problem Description
There are apple trees planted along a cyclic road, which is metres long. Your storehouse is built at position on that cyclic road. The th tree is planted at position x_i, clockwise from position . There are delicious apple(s) on the th tree. You only have a basket which can contain at most apple(s). You are to start from your storehouse, pick all the apples and carry them back to your storehouse using your basket. What is your minimum distance travelled? $1\le n,k\le 10^5, a_i\ge 1, a_1+a_2+...+a_n\le 10^5$ There are less than 20 huge testcases, and less than 500 small testcases.
Input
First line: , the number of testcases. Then testcases follow. In each testcase: First line contains three integers, . Next lines, each line contains .
Output
Output total distance in a line for each testcase.
Sample Input
2
10 3 2
2 2
8 2
5 1
10 4 1
2 2
8 2
5 1
0 10000
Sample Output
18
26
Author
XJZX
Source
2015 Multi-University Training Contest 2