Problem 520
Maximum Sum of Two Non-Overlapping Subarrays
Medium · 20 pts · Array, Prefix Sum, Sliding Window
Problem
Choose two blocks of consecutive elements that do not overlap: one of length exactly L and one of length exactly M. The L-block may come before or after the M-block. Find the largest possible total of the two blocks. Scan once with prefix sums, keeping the best L-block seen so far before the current M-block and the best M-block before the current L-block.
Input
The first line contains N, L and M. The second line contains N non-negative integers.
Output
Print the maximum total of the two subarrays.
Constraints
2 <= N <= 1000; 1 <= L, M; L + M <= N; 0 <= A[i] <= 1000
Example input
9 1 2 0 6 5 2 2 5 1 9 4
Example output
20
Explanation
In 0 6 5 2 2 5 1 9 4 with L = 1 and M = 2, take the single element 9 and the pair 6 5. Together they give 9 + 11 = 20, the largest possible total.