#P11303. Minimizing Algae Consumption
Minimizing Algae Consumption
Minimizing Algae Consumption
Syrup is concerned about the rapid growth of algae in a pond. After a heavy rain, nutrient‐rich soil flows into the pond and gives rise to an explosive growth of algae at discrete soil erosion points. The pond has N erosion points labeled from 1 to N arranged in a line. The distance between point i and point i+1 is given as \(D_i\) (for \(1 \le i \le N-1\)).
Initially, each point has 0 algae. However, every second, 1 new algae grows at each point until Syrup arrives and eats all the algae present there. Syrup starts at point K at time 0 (thus immediately clearing point K) and must choose one direction (either towards point 1 or towards point N). Once a direction is chosen, he swims continuously in that direction and clears the algae at each encountered point.
For example, if Syrup chooses to swim left, he will visit points K-1, K-2, …, 1 in that order. When visiting a point, the number of algae there equals the time elapsed since the start (which is the sum of the distances traveled so far). The total algae consumed is the sum over all visited points of their respective arrival times.
Your task is to compute the minimum total number of algae that Syrup will have to eat by choosing the optimal direction.
The formulas for the two possible directions are as follows:
- When going left (if \(K > 1\)): \(Total_{left} = D_{K-1} + (D_{K-1}+D_{K-2}) + \cdots + (D_{K-1}+D_{K-2}+\cdots+D_{1})\).
- When going right (if \(K < N\)): \(Total_{right} = D_{K} + (D_{K}+D_{K+1}) + \cdots + (D_{K}+D_{K+1}+\cdots+D_{N-1})\).
If one of the directions is not available (i.e. starting at point 1 or point N), Syrup has no choice but to follow the available direction. Output the minimum total algae that Syrup ends up eating.
inputFormat
The first line contains two integers N and K (\(2 \le N \le 10^5\) and \(1 \le K \le N\)).
The second line contains \(N-1\) integers \(D_1, D_2, \dots, D_{N-1}\) representing the distances between consecutive points. All distances are positive integers.
outputFormat
Output a single integer representing the minimum total number of algae consumed by Syrup.
sample
2 1
5
5