P17129 [ICPC 2025 Shanghai R] Round screws

Description

The NIT likes round screws. He also likes the $\oplus$ operator because it reminds him of round screws, where $\oplus$ represents Bitwise-XOR operation. Define the value $V_a$ of a sequence $a_1, a_2, \cdots, a_n$ as $V_a = a_1 + a_n + \sum_{i=1}^{n-1} (a_i \oplus a_{i+1})$. Given a sequence $a_1, a_2, \cdots, a_n$, you can perform the following operation for arbitrary times: - Select an index $i$ ($1 \le i \le n$), change $a_i$ to any non-negative integer; this operation has a cost $C$. Minimize the sum of the value of the sequence and the cost incurred by operations. In other words, let $p$ be the number of operations you performed, and $V_{a'}$ be the value of the $a$ after the operations, then you need to minimize $pC + V_{a'}$.

Input Format

The input contains multiple testcases. The first line of the input contains an integer $T$ ($1 \le T \le 100$), the number of testcases. For each testcase, the first line contains two integers $n, C$ ($2 \le n \le 10^5, 0 \le C < 2^{19}$), the length of the sequence and the cost of performing an operation. The second line contains $n$ integers $a_1, a_2, \cdots, a_n$ ($0 \le a_i < 2^{18}$), representing the elements in the sequence. It’s guaranteed that the sum of $n$ over all testcases does not exceed $2 \times 10^5$.

Output Format

For each testcase, print an integer in $1$ line, the minimum possible value of $pC + V_{a'}$.

Explanation/Hint

For the $1$st testcase, one way to achieve minimum is to change the sequence to $[1,\textbf{1},\textbf{1},\textbf{0}]$; the bold numbers are the changed. The final sequence value is $2$, and $3$ operations are done; the total value $+$ cost is $2 + 3 \times 4 = 14$. For the $2$nd testcase, one way to achieve minimum is to change the sequence to $[6,6,6,\textbf{6},\textbf{6},6,6,6]$; the final value is $12 + 2 \times 6 = 24$.