https://codeforces.com/problemset/problem/1787/C
This is the reason why the problem was named as Remove the Bracket.

Product=a1a2a3an= =a1(x2+y2)(x3+y3)(xn1+yn1)an= =?a1x2+y2x3+y3xn1+yn1an.

(xis)(yis)0 tells us either min(xi,yi) s or max(xi,yi) s, so pickable xi is a consecutive range.$

Just consider (xi+yi), remove the bracket then it turns to \ldots+ y_{i-1}\cdot x_i+y_i\cdot x_{i+1}+\ldots. When y_{i-1} = x_{i+1}, the result is constant, so we assume that yi1<xi+1.

If x_i does not hit the maximum, increase xi by 1 and decrease yi by 1, the result will increase by yi1 and decrease by xi+1, which means decrease by xi+1yi1, always better. So xi will either hit the maximum or the minimum. Thus, each number has only two ways of rewriting that might be optimal.

This can be done with DP.

#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long f[N][2],x[N],y[N];
void get() {
	int i,n,s,j;
	cin>>n>>s;
	for(i=1; i<=n; i++) {
		cin>>j;
		if(i==1||i==n) x[i]=y[i]=j;
		else if(j<=s) x[i]=0,y[i]=j;
		else x[i]=s,y[i]=j-s;
	}
	f[1][0]=f[1][1]=0;
	for(i=2; i<=n; i++) {
		f[i][0]=min(f[i-1][0]+y[i-1]*x[i],f[i-1][1]+x[i-1]*x[i]);
		f[i][1]=min(f[i-1][0]+y[i-1]*y[i],f[i-1][1]+x[i-1]*y[i]);
	}
	cout<<f[n][0]<<endl;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T; cin>>T;
	while(T--) get();
	return 0;
}