#P4272. [CTSC2009] 序列变换

[CTSC2009] 序列变换

Description

Given an integer sequence X1,X2,X3,,XNX_1, X_2, X_3, \dots, X_N and three positive integers Q,A,BQ, A, B that satisfy:

  • 1XiQ1 \leq X_i \leq Q for any 1iN1 \leq i \leq N;
  • XiXi+1X_i \leq X_{i+1} for any 1i<N1 \leq i < N;
  • AQ1N1A \leq \frac{Q - 1}{N - 1} and ABA \leq B.

For any 1iN1 \leq i \leq N, perform the transformation Yi=Xi+δiY_i = X_i + \delta_i, where δi\delta_i is an integer, such that the new sequence YY satisfies:

  • 1YiQ1 \leq Y_i \leq Q for any 1iN1 \leq i \leq N;
  • Yi+1Yi[A,B]Y_{i+1} - Y_i \in [A, B] for any 1i<N1 \leq i < N.

For such a transformation, the required cost is defined as $\operatorname{TransformCost}(X, Y) = \sum_{i = 1}^{N}{\left\lvert\delta_i\right\rvert}$.

The task is to find a transformation that minimizes TransformCost(X,Y)\operatorname{TransformCost}(X, Y).

Input Format

The first line contains 44 integers N,Q,A,BN, Q, A, B.
The second line contains NN integers, namely X1,X2,,XNX_1, X_2, \dots, X_N.

Output Format

Output a single line with the minimal TransformCost(X,Y)\operatorname{TransformCost}(X, Y).

3 6 2 2
1 4 6
1

Hint

Sample Explanation.

You can transform the sequence to 2 4 62\ 4\ 6 or 1 3 51\ 3\ 5. The former has a cost of 11, and the latter has a cost of 22. Therefore, the minimal TransformCost\operatorname{TransformCost} is 11.

Constraints.

For 10%10\% of the testdata, N100N \leq 100, Q10000Q \leq 10000, 1A,B1001 \leq A, B \leq 100.
For 30%30\% of the testdata, N10000N \leq 10000, Q10000Q \leq 10000, 1A,B1001 \leq A, B \leq 100.
For 60%60\% of the testdata, N10000N \leq 10000, Q109Q \leq 10^9, 1A,BQ1 \leq A, B \leq Q.
For 100%100\% of the testdata, N500000N \leq 500000, Q109Q \leq 10^9, 1A,BQ1 \leq A, B \leq Q.

Translated by ChatGPT 5