Need help with LMiO 2019 Potatoes and Fertilizers

Правка en1, от chithanh_nguyen, 2026-06-11 19:50:42

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!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский chithanh_nguyen 2026-06-12 07:08:11 1 Tiny change: ' come up a approach ' -> ' come up an approach '
en5 Английский chithanh_nguyen 2026-06-11 19:56:36 1 Tiny change: 'will crossthe gap be' -> 'will cross the gap be'
en4 Английский chithanh_nguyen 2026-06-11 19:56:03 0 (published)
en3 Английский chithanh_nguyen 2026-06-11 19:55:50 24
en2 Английский chithanh_nguyen 2026-06-11 19:54:57 334
en1 Английский chithanh_nguyen 2026-06-11 19:50:42 913 Initial revision (saved to drafts)