0-1 Hole

mediumdynamic programmingsubarrayalgorithms

From an Array of $$$N$$$ integers you select all possible contagious subarrays. For each possible subarray you are allowed to choose at most one of the integer and remove it. Out of all such possibilities, you need to find out a selection of a contagious subarray and choice of removing one integer which results in $$$\text{maximum}$$$ possible sum.

Please note, you can also choose, not to remove any integer.

Input Format:

The first line contains an integer $$$ N $$$ ($$$ 1 \le N \le 10^5 $$$), representing the number of integers in the given array.

The second line contains $$$N$$$ space separated values as elements of array, where the $$$i$$$-th value represents to $$$a[i]$$$ $$$(-10^4 \leq a[i] \leq 10^4)$$$.

Output Format:

Output a single integer, denoting $$$\text{maximum}$$$ possible sum as described in question.

Examples:

Example 1:

Input:

6
1 2 3 -100 3 2

Output:

11

Example 2:

Input:

6
4 3 2 -1 3 -2

Output:

12

Note:

For first sample test case where $$$ N = 6 $$$ and the array is $$$ [1, 2, 3, -100, 3, 2] $$$ perform the deletion operation on $$$-100 $$$ select the remaining elements of subarray from index $$$0$$$ to $$$5$$$ . The sum of subarray elements is $$$ 1 + 2 + 3 + 3 + 2 = 11 $$$.

Code

Loading Editor...