<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:
    vector<vector<int>> dp;
    int n;
    int solve(int ind, int dlt,vector<int> &arr){
        //if dlt ==1 can dlt one
        if(ind>=n) return 0;
        if(dp[ind][dlt]!=-1) return dp[ind][dlt];

        int ans=0;
        if(dlt){
            //first way : pick current and go ahead
            int first =arr[ind]+solve(ind+1,dlt,arr);

            //second way : pick current and end
            int second = arr[ind];

            //third way: dlt current and go ahead
            int third = solve(ind+1,0,arr); //dltd hence dlt becomes 0

            //fourth way: dont pick current and go back , it will be handled by ans =0 initialisation

            ans =max(ans,max(first,max(second,third)));
        }
        else{
            //first way: pick current go ahead
            int first = arr[ind] + solve(ind+1,dlt,arr);

            //second way: pick current and leave 
            int second = arr[ind];

            //third way : dont pick and leave, handled automatically by ans =0;
            ans=max(ans,max(first,second));
        }

        return dp[ind][dlt]=ans;

        
    }
    int maximumSum(vector<int>& arr) {
        //i can end on current element or can go ahead
        //subarray can start from any possible index
        //2d dp
        n =arr.size();
        int maxElement = *max_element(arr.begin(), arr.end());
        if(maxElement<0) return maxElement;
        dp.resize(n,vector<int>(2,-1));
        int ans = 0;
        for(int i =0;i<n ;i++){
            ans =max(ans,solve(i,1,arr));
        }
        
        return ans;
    }
};
46200