Description
Solutions
Maximize Sum of Two Non-Overlapping Fragments
πŸ‘©β€πŸŽ“ NEW GRAD

There is an array A of N integers which may contain positive and negative values. Choose two fragments, one of length K and one of length L, to maximize the sum of the elements that belong to the chosen fragments. However, if any individual element belongs to both fragments at the same time, each such element is added to the final sum only once and with a changed sign (i.e., negative values become positive values, and vice versa).

Write a function:

def solution(A, K, L)

that, given an array A of N integers and integers K and L, returns the maximum total sum that can be obtained.

Example 1:

Input:  A = [1, 3, -4, 2, -1], K = 3, L = 2
Output: 10
Explanation:

For A = [1, 3, -4, 2, -1], K = 3, L = 2, you can choose fragment [1, 3, -4] and fragment [-4, 2]. The third element of A belongs to both fragments, so the function should return 1 + 3 + -(-4) + 2 = 10.

Example 2:

Input:  A = [-5, -3, -4], K = 1, L = 3
Output: -2
Explanation:

For A = [-5, -3, -4], K = 1, L = 3, the segment of length L will contain each element of A. Then choosing -5 as the only element of the K segment will change its sign in the final sum. The function should return -2.

Constraints:
    :)
Thumbnail 0
Testcase

Result
Case 1

input:

output: