I, O Bot

아직 제출이 없습니다시간 제한40초메모리 제한1024 MB

문제

To welcome attendees to a developers' conference on Jupiter's moon of Io, the organizers inflated many giant beach balls. Each ball is in roughly the shape of either a 11 or a 00, since those look sort of like the letters I and O. The conference just ended, and so now the beach balls need to be cleaned up. Luckily, the beach ball cleanup robot, BALL-E, is on the job!

The conference was held on an infinite horizontal line, with station 00 in the middle, stations 1,2,1, 2, \dots to the right, and stations 1,2,-1, -2, \dots to the left. Station 0 contains the conference's only beach ball storage warehouse. Each other station contains at most one beach ball.

BALL-E has two storage compartments, each of which can hold a single beach ball. One compartment can only hold 11⁠-shaped balls and the other can only hold 00⁠-shaped balls. (The 11⁠-shaped balls are more oblong than the 00⁠-shaped balls, so neither shape of ball will fit in the other shape's compartment.)

BALL-E initially has both the 00 and 11 compartments empty, and it starts off at station 00. The robot can do the following things:

  • Move left one station or right one station. This costs 1 unit of power.
  • If there is a ball at the current station, and BALL-E is not already storing a ball of that shape, it can put the ball in the appropriate compartment. This takes 0 units of power.
  • If there is a ball at the current station, BALL-E can compress it so that its shape becomes the other shape. That is, a 11⁠-shaped ball becomes a 00⁠-shaped ball, or vice versa. It takes C\mathbf{C} units of power to do this. Note that BALL-E may not change the shape of a ball that it has already put into one of its compartments.
  • If BALL-E is at station 0 and is storing at least one ball, it can deposit all balls from its compartment(s) into the beach ball storage warehouse. This takes 0 units of power and leaves both compartments empty.

Notice that if BALL-E moves to a station and there is a ball there, BALL-E is not required to pick it up immediately, even if the robot has an open compartment for it. Also, if BALL-E moves to the station with the warehouse, it is not required to deposit any balls it has.

Find the minimum number of units of power needed for BALL-E to transfer all of the balls to the warehouse, using only the moves described above.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} test cases follow. The first line of each test case contains two integers, N\mathbf{N} and C\mathbf{C}: the number of balls and the amount of power units needed to change the shape of a ball, respectively. The next N\mathbf{N} lines describe the positions (i.e., station numbers) and the shapes of the balls. The ii-th line contains two integers, X_i\mathbf{X\_i} and S_i\mathbf{S\_i}: the position and the shape of the ii-th ball, respectively.

출력

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the minimum number of units of power needed to transfer all of the balls to the warehouse, as described above.

제한

  • 1T1001 \le \mathbf{T} \le 100.
  • 0S_i10 \le \mathbf{S\_i} \le 1, for all ii.
  • 109X_i109-10^9 \le \mathbf{X\_i} \le 10^9, for all ii.
  • 0C1090 \le \mathbf{C} \le 10^9.
  • X_i0\mathbf{X\_i} ≠ 0, for all ii.
  • All X_i\mathbf{X\_i} are distinct.

힌트

In Sample Case #1 (illustrated in the statement), there are N=5\mathbf{N} = 5 balls and C=0\mathbf{C} = 0. One optimal strategy is to make three round trips from (and back to) the warehouse:

  • First round trip: Move to station 33, pick up the 00 ball there and store it in the 00 compartment, move back to station 00, and deposit the ball in the warehouse. This takes 66 units of power.
  • Second round trip: Move to station 88, pick up the 00 ball there, and store it in the 00 compartment. Move to station 66, change the 00 ball there into a 11 ball, and pick it up and store it in the 11 compartment. Move to station 00 and deposit both balls in the warehouse. This takes 1616 units of power. (Recall that in this case, it takes 00 units of power to change a ball's shape.)
  • Third round trip: Move to station 1010. Change the 11 ball there into a 00 ball, and pick it up and store it in the 00 compartment. Move to station 1515. Pick up the 11 ball there and store it in the 11 compartment. Move to station 00 and deposit both balls in the warehouse. This takes 3030 units of power.

The total number of units of power needed to collect all the balls is 5252.

Sample Case #2 is like Sample Case #1, but now with C=10\mathbf{C} = 10. Now BALL-E has to use at least 5656 units of power:

  • First round trip: Get the ball from station 33. This takes 66 units of power.
  • Second round trip: Get the differently-shaped balls from stations 66 and 1010. (These are a 00 and a 11, respectively, so there is no need to change the shape of either of them.) This takes 2020 units of power.
  • Third round trip: Get the differently-shaped balls from stations 88 and 1515. This takes 3030 units of power.

Sample Case #3 is also like Sample Case #1, but now with C=1\mathbf{C} = 1. Here, BALL-E needs at least 5454 units of power:

  • First round trip: Get the ball from station 33. This takes 66 units of power.
  • Second round trip: Get the ball from station 88. When passing through station 66 on the way back, change the shape of the ball there and get it. This takes 1717 units of power.
  • Third round trip: Do the same with the balls at stations 1515 and 1010. This takes 3131 units of power.

In Sample Case #4, one optimal strategy is for BALL-E to move to station 1000000000-1000000000, get the 11 ball there, move to station 10000000001000000000, get the 00 ball there, and then return to station 00 to deposit both of them.