<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>

class Solution {
public:
    int memo[(int)1e5+2][2][2];
    int dp(int i,int cont,vector<int> &arr,int skipped){
        if(i>=arr.size()){
            return 0;
        }
        if(memo[i][cont][skipped]!=(int)-1e9-10){
            return memo[i][cont][skipped];
        }
        int choice1 = arr[i] + dp(i+1,true,arr,skipped);
        int choice2 = 0;
        int choice3 = 0;
        if(skipped>0 && cont){
            choice2 =  dp(i+1,true,arr,0);
        }
        if(cont){
            return max(choice1,choice2);
        }
        choice3 = dp(i+1,false,arr,1);
        return memo[i][cont][skipped]= max(choice1,max(choice2,choice3));
    }

    int maximumSum(vector<int>& arr) {
        // for(int i=0;i<(int)1e5+1;i++){
        //     for(int j=0;j<2;j++){
        //         for(int k=0;k<2;k++){
        //             memo[i][j][k] = (int)-1e9-10;
        //         }
        //     }
        // }

        int n = arr.size();
        for(int i=n;i>=0;i--){
            for(int cont=0;cont<=1;cont++){
                for(int skipped=0;skipped<=1;skipped++){
                    if(i>=n){
                        memo[i][cont][skipped] = 0;
                        continue;
                    }
                    int choice1 = arr[i] + memo[i+1][true][skipped];
                    int choice2 = 0;
                    int choice3 = 0;
                    if(skipped>0 && cont){
                        choice2 =  memo[i+1][true][0];
                    }
                    if(cont){
                        memo[i][cont][skipped] =  max(choice1,choice2);
                        continue;
                    }
                    choice3 = memo[i+1][false][1];
                    memo[i][cont][skipped]= max(choice1,max(choice2,choice3));
                }
            }
        }
        // int ans1 =  dp(0,false,arr,1);
        int ans1 = memo[0][false][1];
        int ans2 = INT_MIN;
        for(int i=0;i<n;i++){
            ans2 = max(ans2,arr[i]);
        }
        if(ans1==0){
            return ans2;
        }
        return ans1;
    }
};
27700