I recently came up with this problem, I'd really appreciate feedback from experienced problem setters and contestants, any attempt to break this problem, simplify it, or find flaws in the intended solution:
THIS IS STILL ONLY A PROBLEM IDEA WITH A PROPOSED SOLUTION SKETCH AND MAY OR MAY NOT HAVE 998244353 FLAWS
Prefix-Suffix Levelling
Statement
You are given an array $$$A$$$ of $$$N$$$ integers, and two positive cost arrays $$$P$$$ and $$$S$$$ of size $$$N$$$.
In one step, you can choose an index $$$i$$$ ($$$1 \le i \le N$$$) and perform one of the following two operations: 1. Prefix Addition: Add $$$+1$$$ to all elements in the prefix $$$A[1 \dots i]$$$, paying a cost of $$$P_i$$$. 2. Suffix Addition: Add $$$+1$$$ to all elements in the suffix $$$A[i \dots N]$$$, paying a cost of $$$S_i$$$.
Find the minimum total cost to make all elements of $$$A$$$ non-negative ($$$A_k \ge 0$$$ for all $$$1 \le k \le N$$$).
Input Format
The first line contains a single integer $$$N$$$ ($$$1 \le N \le 5 \cdot 10^5$$$) — the size of the arrays.
The second line contains $$$N$$$ integers $$$A_1, A_2, \dots, A_N$$$ ($$$-10^9 \le A_i \le 10^9$$$) — the initial array values.
The third line contains $$$N$$$ integers $$$P_1, P_2, \dots, P_N$$$ ($$$1 \le P_i \le 10^9$$$) — the prefix operation costs.
The fourth line contains $$$N$$$ integers $$$S_1, S_2, \dots, S_N$$$ ($$$1 \le S_i \le 10^9$$$) — the suffix operation costs.
Output Format
Print a single integer — the minimum total cost required to make all elements of $$$A$$$ non-negative.
Sample Cases
Sample 1
Input
3
-4 -6 -3
6 2 7
7 2 9
Output
14
Explanation
Initially $$$A = [-4, -6, -3]$$$. * Apply prefix operation at $$$i = 2$$$ with magnitude $$$4$$$ (cost: $$$4 \times P_2 = 4 \times 2 = 8$$$).
$$$A$$$ becomes $$$[-4 + 4, -6 + 4, -3] = [0, -2, -3]$$$. * Apply suffix operation at $$$i = 2$$$ with magnitude $$$3$$$ (cost: $$$3 \times S_2 = 3 \times 2 = 6$$$).
$$$A$$$ becomes $$$[0, -2 + 3, -3 + 3] = [0, 1, 0]$$$. All elements are now non-negative. Total cost: $$$8 + 6 = 14$$$.
Sample 2
Input
4
-5 -2 -6 -4
10 3 8 12
4 9 2 7
Output
22
Explanation
We perform: * $$$5$$$ suffix operations at index $$$1$$$ (cost $$$5 \times S_1 = 5 \times 4 = 20$$$). This adds $$$+5$$$ to all elements $$$A[1 \dots 4]$$$. * $$$1$$$ suffix operation at index $$$3$$$ (cost $$$1 \times S_3 = 1 \times 2 = 2$$$). This adds $$$+1$$$ to elements $$$A[3 \dots 4]$$$. The resulting array is:
All elements are non-negative. Total cost: $$$20 + 2 = 22$$$.
Sample 3
Input
3
5 0 10
1 1 1
1 1 1
Output
0
Explanation
All elements are already non-negative, so no operations are required (cost $$$0$$$).








Dual LP? Hardly seen on Codeforces, but usually seen in Problem G of ABC?
Yep! Bridging that clean CF array statement with an ABC math reduction apparently is what makes the $$$O(N \log N)$$$ payoff fun
I m a newbie so dont understand much,but this question seems good level of div3 D/E or div 2 c/d Good question mate.
easiest codeforces div3D:
It just might be a 1200-1400 rated problem
Like we are storing two SegTrees:
MinSegTree, which stores the minimum value for prefix and suffix for one index.
ArraySegTree, which stores the array after operations.
We storing all negative numbers in array with their index, then sorting them by the minimum value in MinSegTree for this index. Then, one by one, updating both SegTrees:
Apply operation for $$$a[i]$$$ and update array (with LazySegTree)
Repeat for every i in negative_number_indexes.
Done!
That's a good natural greedy intuition (and apparently the exact trap lol). However, choosing operations locally based on the cheapest operation for a single index fails because it misses range coverage synergy, paying slightly more for a wider prefix/suffix operation can fix multiple negative elements simultaneously at a much lower total cost.
Here is a simple counter-example where the SegTree greedy fails:
$$$N = 3$$$$$$A = [-5, -1, -5] $$$$$$P = [4, 100, 5]$$$ (Prefix costs) $$$S = [100, 100, 100]$$$ (Suffix costs)
To fix $$$A_1 = -5$$$, it picks the locally cheapest option $$$P_1$$$ (cost $$$4$$$) and applies it 5 times $$$\rightarrow$$$ Cost = $$$20$$$. (Array becomes $$$[0, -1, -5]$$$). To fix $$$A_3 = -5$$$, it picks $$$P_3$$$ (cost $$$5$$$) and applies it 5 times $$$\rightarrow$$$ Cost = $$$25$$$.Total Greedy Cost = $$$45$$$
:sob:
congrats on reaching expert!!
Thanks <3