MicrosoftOA #39
Maximize Sum of Two Non-Overlapping Fragments
Estimated HardJudge-ready
Topics (AI-labelled)Prefix & RangesTwo Pointers
Choose two contiguous segments of lengths K and L; they may overlap. An element in only one segment counts at its original value, while an element in the overlap counts only once with its sign flipped. The title's "non-overlapping" conflicts with the statement, so this site follows the overlap sign-flip rule given by the statement and both examples.
Input
This site's ranges: the first line contains N K L (1≤N≤2000, 1≤K,L≤N), and the second line contains N integers (−10⁹..10⁹).
Output
Output a single integer, the answer.
Example 1
Input:
5 3 2
1 3 -4 2 -1
Output:
10
Example 2
Input:
3 1 3
-5 -3 -4
Output:
-2
Example 3
Input:
2 1 1
1 2
Output:
3
Example notes
Example 1: choose indices 0..2 and 2..3; −4 becomes 4, for a total of 1+3+4+2=10.
Example 2: the whole array overlaps the single element −5, turning −5 into 5, for a total of 5−3−4=−2.
Example 3: choose 1 and 2 separately with no overlap, for a total of 3.
Editorial
Loading solution…