#P2748. Garden Soil Adjustment
Garden Soil Adjustment
Garden Soil Adjustment
Farmer John is planning to build a new garden which has flowerbeds. In the -th flowerbed, there are currently units of soil, but he needs exactly 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|$.
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 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 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 , , , and , where .
The second line contains integers representing the current soil units in each flowerbed.
The third line contains integers 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