TimeLimit: 5000/3000 MS (Java/Others) MemoryLimit: 524288/524288 K (Java/Others)
64-bit integer IO format:%I64d
未提交 | 登录后收藏
Problem Description
There are n apple trees planted along a cyclic road, which is L metres long. Your storehouse is built at position 0 on that cyclic road.
The ith tree is planted at position x_i, clockwise from position 0. There are ai delicious apple(s) on the i-th tree.
You only have a basket which can contain at most K 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≤n,k≤105,ai≥1,a1+a2+...+an≤105 1≤L≤109 0≤x[i]≤L
There are less than 20 huge testcases, and less than 500 small testcases.
Input
First line: t, the number of testcases.
Then t testcases follow. In each testcase:
First line contains three integers, L,n,K.
Next n lines, each line contains xi,ai.
Output
Output total distance in a line for each testcase.