1787C.md (1686B)
1 Source: https://codeforces.com/problemset/problem/1787/C 2 3 C. Remove the Bracket 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 RSJ has a sequence $a$ of $n$ integers $a_1,a_2, \ldots, a_n$ and an integer $s$. For each of $a_2,a_3, \ldots, a_{n-1}$, he chose a pair of non-negative integers $x_i$ and $y_i$ such that $x_i+y_i=a_i$ and $(x_i-s) \cdot (y_i-s) \geq 0$. 22 23 Now he is interested in the value $$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.$$ 24 25 Please help him find the minimum possible value $F$ he can get by choosing $x_i$ and $y_i$ optimally. It can be shown that there is always at least one valid way to choose them. 26 27 Input 28 29 Each test contains multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. 30 31 The first line of each test case contains two integers $n$, $s$ ($3 \le n \le 2 \cdot 10^5$; $0 \le s \le 2 \cdot 10^5$). 32 33 The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 2 \cdot 10^5$). 34 35 It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$. 36 37 Output 38 39 For each test case, print the minimum possible value of $F$. 40 41 Example 42 43 Input 44 45 ``` 46 10 47 48 5 0 49 50 2 0 1 3 4 51 52 5 1 53 54 5 3 4 3 5 55 56 7 2 57 58 7 6 5 4 3 2 1 59 60 5 1 61 62 1 2 3 4 5 63 64 5 2 65 66 1 2 3 4 5 67 68 4 0 69 70 0 1 1 1 71 72 5 5 73 74 4 3 5 6 4 75 76 4 1 77 78 0 2 1 0 79 80 3 99999 81 82 200000 200000 200000 83 84 6 8139 85 86 7976 129785 12984 78561 173685 15480 87 ``` 88 89 Output 90 91 ``` 92 0 93 18 94 32 95 11 96 14 97 0 98 16 99 0 100 40000000000 101 2700826806 102 ``` 103 104 Note 105 106 In the first test case, $2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0$. 107 108 In the second test case, $5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18$.