Link to the problem: [https://oj.uz/problem/view/LMIO19_bulves](click here)
Summary. Given two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. In one operation, we can move one unit from $$$a_i$$$ to an adjacent position. We need to find the minimum number of operations such that $$$a_i \geq b_i$$$ for all $$$i$$$. It is guaranteed that a solution always exists.
Constraints. $$$n \leq 5\cdot 10^5; a_i, b_i \leq 10^6$$$.
I have already come up a approach for the subtasks with $$$\sum_{i = 1}^n a_i = \sum+{i = 1}^n b_i$$$. Let $$$pref_i = \sum_{i = 1}^n (a_i - b_i)$$$, then $$$|pref_i|$$$ units must cross the edge between $$$i$$$ and $$$i + 1$$$. This implies that the answer will be $$$\sum_{i = 1}^n |pref_i|$$$.
However, I am stuck on how to generalize this to the full problem and also could not understand the accepted submissions. Any hints or explanations would be appreciated. Thanks!



