Need help with LMiO 2019 Potatoes and Fertilizers

Revision en5, by chithanh_nguyen, 2026-06-11 19:56:36

Link to the problem: 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$$$, when the target become to make $$$a_i = b_i$$$ for all $$$i$$$.

Let $$$pref_i = \sum_{i = 1}^n (a_i - b_i)$$$. If $$$pref_i \gt 0$$$, it means that the left side has $$$pref_i$$$ extra units, so we have to move them to the right side. Otherwise, the left side is missing of units, so we have to move $$$-pref_i$$$ units from the right side to the left side. In both cases, exactly $$$|pref_i|$$$ units will cross the gap between positions $$$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!

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en6 English chithanh_nguyen 2026-06-12 07:08:11 1 Tiny change: ' come up a approach ' -> ' come up an approach '
en5 English chithanh_nguyen 2026-06-11 19:56:36 1 Tiny change: 'will crossthe gap be' -> 'will cross the gap be'
en4 English chithanh_nguyen 2026-06-11 19:56:03 0 (published)
en3 English chithanh_nguyen 2026-06-11 19:55:50 24
en2 English chithanh_nguyen 2026-06-11 19:54:57 334
en1 English chithanh_nguyen 2026-06-11 19:50:42 913 Initial revision (saved to drafts)