#P5016. Minimizing Momentum Difference in Dragon-Tiger Game
Minimizing Momentum Difference in Dragon-Tiger Game
Minimizing Momentum Difference in Dragon-Tiger Game
In the Dragon-Tiger game, there is a linear board with n barracks, numbered from 1 to n from left to right. Adjacent barracks are 1 cm apart so that the board forms a segment of length n-1 cm. The i-th barrack initially has c_i soldiers. One special barrack, number m, acts as the boundary: barracks strictly to the left of m (i.e. indices 1 through m-1) belong to the Dragon side (left) and those strictly to the right of m (i.e. indices m+1 through n) belong to the Tiger side (right). The soldiers in the m-th barrack are neutral and do not count for either side.
The momentum (\(\text{势}\)) of a barrack is defined as:
[ \text{momentum}(i) = c_i \times |i - m| ]
Thus, the total momentum of the Dragon side is the sum of the momenta of barracks i with \(i m\).
During the game, an event occurs: at a certain moment, s\(_1\) soldiers suddenly appear at the p\(_1\)-th barrack, so that its new soldier count becomes \(c_{p_1} + s_1\). Then, you have s\(_2\) additional soldiers at your disposal. You can choose any barrack p\(_2\) (from 1 to n) to deploy all these soldiers. Note that if soldiers are deployed in a barrack, they join the other soldiers there and adopt the same side affiliation as the barrack: if p\(_2\) \( < m\), they join the Dragon side, if p\(_2\) \( > m\), they join the Tiger side, and if p\(_2\) equals m they remain neutral.
Your task is to choose the optimal barrack p\(_2\) for deployment to minimize the absolute difference in momentum between the two sides after all changes. Formally, let the updated soldier counts be \(c'_i\) where \(c'_{p_1} = c_{p_1} + s_1\) and for any deployment at barrack \(p_2\), if \(p_2 \neq m\), an extra \(s_2\) soldiers are added. The final momenta become:
- If \(p_2 < m\): \[ \text{Dragon} = \sum_{i=1}^{m-1} c'_i \times (m-i) + s_2 \times (m-p_2),\quad \text{Tiger} = \sum_{i=m+1}^{n} c'_i \times (i-m). \]
- If \(p_2 > m\): \[ \text{Dragon} = \sum_{i=1}^{m-1} c'_i \times (m-i),\quad \text{Tiger} = \sum_{i=m+1}^{n} c'_i \times (i-m) + s_2 \times (p_2-m). \]
- If \(p_2 = m\): the extra soldiers are neutral and do not contribute; thus the momenta remain unchanged.
Determine the minimum possible absolute difference \(|\text{Dragon} - \text{Tiger}|\) achievable by choosing an appropriate p\(_2\).
inputFormat
The input consists of four lines:
- The first line contains two integers n and m (\(2 \le n \le 10^5\), \(1 \lt m \lt n\)).
- The second line contains n integers \(c_1, c_2, ..., c_n\) representing the initial number of soldiers in each barrack.
- The third line contains two integers p1 and s1 indicating that at barrack p1, s1 soldiers suddenly appear (\(1 \le p_1 \le n\)).
- The fourth line contains an integer s2 representing the number of soldiers you can deploy.
outputFormat
Output a single integer, the minimum absolute difference in momentum between the Dragon and Tiger sides after all changes.
sample
6 4
1 2 3 4 5 6
2 10
5
3