Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author: yzhao123
Subtask 1
This subtask was meant for contestants to try and solve
by hand.
Subtask 2
There are numerous ways to solve this problem. The intended solution will be described here. Each index
has
subarrays ending there, and we will make those subarrays all nonnegative or all negative. It is always possible to partition the integers
into two sets with a difference in sum of at most
. Let
represent
nonnegative sum subarrays ending at index
, and
represent
negative sum subarrays ending at index
. For even
, we use the pattern
with signs repeating
which has a difference of at most
at each even index. For odd
, we use the pattern
with signs repeating
which has a difference of at most
at each odd index. The implementation to create this array is left as an exercise to a reader.
Comments