<p>Given an array of integers, return the maximum sum for a <strong>non-empty</strong>&nbsp;subarray (contiguous elements) with at most one element deletion.&nbsp;In other words, you want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the&nbsp;sum of the remaining elements is maximum possible.</p>

<p>Note that the subarray needs to be <strong>non-empty</strong> after deleting one element.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,-2,0,3]
<strong>Output:</strong> 4
<strong>Explanation: </strong>Because we can choose [1, -2, 0, 3] and drop -2, thus the subarray [1, 0, 3] becomes the maximum value.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arr = [1,-2,-2,3]
<strong>Output:</strong> 3
<strong>Explanation: </strong>We just choose [3] and it&#39;s the maximum sum.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> arr = [-1,-1,-1,-1]
<strong>Output:</strong> -1
<strong>Explanation:</strong>&nbsp;The final subarray needs to be non-empty. You can&#39;t choose [-1] and delete -1 from it, then get an empty subarray to make the sum equals to 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>4</sup> &lt;= arr[i] &lt;= 10<sup>4</sup></code></li>
</ul>

#include<bits/stdc++.h>
bool boolDp[100005][3][2];
int dp[100005][3][2];
class Solution {
public:
    
    int rec(int ind,int count,int next,vector<int> &arr){
        if(ind == arr.size()){
            return 0;
        }
        
        if(boolDp[ind][count][next] == true ){
            return dp[ind][count][next];
        }

        int take = -1e9;
        int take1 = -1e9;
        int take2 = -1e9;
        if(count == 0){
            if(arr[ind] < 0){
                take1 = 0 + rec(ind+1,1,1,arr);
                take2 = arr[ind] + rec(ind+1,count,1,arr);
            }
            else{
                take = arr[ind] + rec(ind+1,count,1,arr);
            }
        }
        else{
            take = arr[ind] + rec(ind+1,count,next,arr);
        }
        
        int newTake = -1e9;

        if(next == 0){
            newTake = rec(ind+1,count,next,arr);
        }
        else{
            newTake = 0;
        }
        boolDp[ind][count][next] = true;
        
        //return max(take,newTake);
        return dp[ind][count][next] = max(max(max(take1,take2),take),newTake);
    }
    int maximumSum(vector<int>& arr) {
        memset(boolDp,false,sizeof(boolDp));
        //sort(arr.begin(),arr.end());
        int ans = rec(0,0,0,arr);
        sort(arr.begin(),arr.end());

        if(ans == 0){
            return arr.back();
        }
        else{
            return ans;
        }
    }
};
42300