beats 100% || considers all conditions seperately
Intuition\nfirstly tried greedy,though of binary search,etc but a regular dp question\n Describe your first thoughts on how to solve this problem. \n\n\n\n# Com
# Intuition\nfirstly tried greedy,though of binary search,etc but a regular dp question\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n# Complexity\n- Time complexity:\n- $$O(n)$$ dp array of 2*n size\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- $$O(n)$$(recursive stack and dp array)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    private long f(int[] nums,long[][] dp,int i,int flip){\n         if(i==nums.length) return 0;\n         if(dp[i][flip]!=-2) return dp[i][flip];\n         if(nums[i]>0 || flip==0){\n             long ans=f(nums,dp,i+1,1);\n             \n            if(nums[i]>=0) {\n                dp[i][0]=ans+nums[i];\n                dp[i][1]=ans+nums[i];\n            }else dp[i][flip]=ans+nums[i];\n             return dp[i][flip];\n             \n         }\n         else {\n           long ans1=f(nums,dp,i+1,1)+nums[i];\n           long ans2=f(nums,dp,i+1,0)+nums[i]*-1;\n           dp[i][0]=ans1;\n           return dp[i][1]=Math.max(ans1,ans2);\n         }\n         \n\n    } \n    public long maximumTotalCost(int[] nums) {\n       long[][] dp=new long[nums.length][2];\n       for(int i=0;i<nums.length;i++) {\n        dp[i][1]=-2;\n        dp[i][0]=-2;\n        }\n       return f(nums,dp,0,0);\n    }\n   \n}\n\n//-7 -5 -2 3 -2 4\n```
['Dynamic Programming', 'Java']