#P16773. [GKS 2020 #G] Combination Lock

[GKS 2020 #G] Combination Lock

Problem Description

A combination lock has WW wheels, each of which has the integer values 11 through NN on it, in ascending order.

At any moment, each wheel shows a specific value on it. XiX_i is the initial value shown on the ii-th wheel.

You can use a single move to change a wheel from showing the value XX to showing either X+1X+1 or X1X-1, wrapping around between 11 and NN. For example, if a wheel currently shows the value 11, in one move you can change its value to 22 or NN.

Given all wheels' initial values, what is the minimum number of moves to get all wheels to show the same value?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains the two integers WW and NN.

The second line contains WW integers. The ii-th integer is XiX_i.

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the minimum number of moves to get all wheels to show the same value.

2
3 5
2 3 4
4 10
2 9 3 8
Case #1: 2
Case #2: 8

Hint

In Sample Case #11, the best solution is to get all wheels to show value 33, which would take a total of 22 moves: the first wheel would move once (from value 22 to value 33), the second wheel would not move (it already shows value 33), and the third wheel would move once (from value 44 to value 33).

For reference, it would take 55 moves to get all wheels to show value 11, 33 moves to get all wheels to show value 22, 33 moves to get all wheels to show value 44, and 55 moves to get all wheels to show value 55.

In Sample Case #22, the best solutions are to get all wheels to show either value 11, 22, 99 or 1010, which would take a total of 88 moves.

Limits

1T1001 \le T \le 100.

1XiN1 \le X_i \le N.

Test Set 11

1W10001 \le W \le 1000.

2N10002 \le N \le 1000.

Test Set 22

1W10001 \le W \le 1000.

2N1092 \le N \le 10^9.

Test Set 33

1W1051 \le W \le 10^5.

2N1092 \le N \le 10^9.