#P2748. Garden Soil Adjustment

    ID: 16011 Type: Default 1000ms 256MiB

Garden Soil Adjustment

Garden Soil Adjustment

Farmer John is planning to build a new garden which has NN flowerbeds. In the ii-th flowerbed, there are currently AiA_i units of soil, but he needs exactly BiB_i units. To achieve this, he can perform the following operations:

  • Buy one unit of soil and add it to a specified flowerbed at a cost of $X$.
  • Remove one unit of soil from any flowerbed at a cost of $Y$.
  • Move one unit of soil from flowerbed $i$ to flowerbed $j$ at a cost of $Z|i-j|$.
It is guaranteed that $1\le N \le 10^5$ and $0 \le A_i, B_i \le 10$. Note that moving soil might be cheaper than removing soil from one bed and buying soil for another if $Z|i-j|

Your task is to compute the minimum total cost needed to adjust the amounts of soil in all flowerbeds to meet the desired configuration.

A useful approach is to calculate the difference Di=AiBiD_i = A_i - B_i for each flowerbed. Positive values mean extra soil that may be removed or transported to other flowerbeds, while negative values indicate a deficit that may be compensated by buying soil. One can then consider the prefix sums fi=j=1iDjf_i = \sum_{j=1}^{i} D_j and derive the cost of transporting soil along the line. In particular, one strategy is:

$$cost_1 = \sum_{i=1}^{N-1} Z|f_i| + \begin{cases} f_N \cdot Y, & \text{if } f_N \ge 0\\ -f_N \cdot X, & \text{if } f_N < 0 \end{cases} $$

Alternatively, if transferring is too expensive, one can handle each flowerbed independently:

$$cost_2 = \sum_{i=1}^{N}\begin{cases} (A_i - B_i)\cdot Y, & \text{if } A_i > B_i\\ (B_i - A_i)\cdot X, & \text{if } A_i < B_i \end{cases} $$

The answer is the minimum of these two costs.

inputFormat

The first line contains four integers NN, XX, YY, and ZZ, where 1N1051\le N\le10^5.
The second line contains NN integers A1,A2,,ANA_1, A_2, \dots, A_N representing the current soil units in each flowerbed.
The third line contains NN integers B1,B2,,BNB_1, B_2, \dots, B_N representing the desired soil units.

outputFormat

Output a single integer representing the minimum cost required to adjust the soil levels to the desired configuration.

sample

2 1 1 1
5 0
0 5
5