Editorial
Consider
The answer is . Thus, using prefix sums and checking every interval solves the problem in time.
Intuition
Suppose from the beginning that some optimal solution has the following form.
If the positive part is the interval , then the condition determines
Proof
Fix any feasible . For a nonnegative real number , keep exactly the indices satisfying . The remaining indices form a disjoint union of contiguous intervals.
Solution written by GPT5.6