Balance - MarisaOJ: Marisa Online Judge

Balance

Time limit: 1500 ms
Memory limit: 256 MB
Given an array $A$ consisting of $n$ integers. In one operation, you can select a number and move $1$ unit to one of its adjacent numbers. Note that you can move from element $1$ to element $n$ and vice versa. Determine the minimum number of operations needed to make all elements in the array equal. ### Input - The first line contains an integer $n$. - The second line contains $n$ integers $A_i$. ### Output - Print an integer, the minimum number of operations. ### Constraints - $1 \leq n \leq 10^6$. - $1 \leq A_i \leq 10^9$. ### Example Input: ``` 4 1 2 5 4 ``` Output: ``` 4 ```