CF BUDDY
← Problems·

1787C · Remove the Bracket

1600 · dp, greedy, math

Problem: RSJ has a sequence aa of nn integers a1,a2,,ana_1,a_2, \ldots, a_n and an integer ss. For each of a2,a3,,an1a_2,a_3, \ldots, a_{n-1}, he chose a pair of non-negative integers xix_i and yiy_i such that xi+yi=aix_i+y_i=a_i and (xis)(yis)0(x_i-s) \cdot (y_i-s) \geq 0.

Now he is interested in the value F=a1x2+y2x3+y3x4++yn2xn1+yn1an.F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.

Please help him find the minimum possible value FF he can get by choosing xix_i and yiy_i optimally. It can be shown that there is always at least one valid way to choose them.

Input Format: Each test contains multiple test cases. The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers nn, ss (3n21053 \le n \le 2 \cdot 10^5; 0s21050 \le s \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (0ai21050 \le a_i \le 2 \cdot 10^5).

It is guaranteed that the sum of nn does not exceed 21052 \cdot 10^5.

Output Format: For each test case, print the minimum possible value of FF.

Note: In the first test case, 20+01+03+04=02\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0.

In the second test case, 51+22+22+15=185\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18.

Sample Cases

Case 1

Input

10
5 0
2 0 1 3 4
5 1
5 3 4 3 5
7 2
7 6 5 4 3 2 1
5 1
1 2 3 4 5
5 2
1 2 3 4 5
4 0
0 1 1 1
5 5
4 3 5 6 4
4 1
0 2 1 0
3 99999
200000 200000 200000
6 8139
7976 129785 12984 78561 173685 15480

Output

0
18
32
11
14
0
16
0
40000000000
2700826806

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback