#K80942. Equalizing Water Sections
Equalizing Water Sections
Equalizing Water Sections
You are given a series of water sections, each with its current water level. In one move, you can transfer 1 unit of water between adjacent sections.
Your task is to calculate the minimum number of moves required to equalize the water levels in all sections. If it is impossible to equalize them, output -1.
Formally, let \(n\) be the number of sections and \(w_i\) be the water level in section \(i\). The goal is to achieve \(w_1 = w_2 = \cdots = w_n = \frac{\sum_{i=1}^{n} w_i}{n}\). If \(\sum_{i=1}^{n} w_i\) is not divisible by \(n\), then equalization is impossible and you should output -1.
inputFormat
The input consists of two lines:
- The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) representing the number of water sections.
- The second line contains \(n\) space-separated integers representing the water levels \(w_i\) of each section.
outputFormat
Output a single integer: the minimum number of moves required to equalize the water levels, or -1 if it is impossible.
## sample3
1 0 5
3