3Dp with state , index and minus 1 as parameters
State represents whether you are in continuation of the previous array or not (1 or 0 respectively).\n\nState is either 0 or 1 we will have 2 choices in both ca
State represents whether you are in continuation of the previous array or not (1 or 0 respectively).\n\nState is either 0 or 1 we will have 2 choices in both cases i.e whether to continue the same state or not.\nend_array means we are ending the array at that index.\ncurr_array that the next call will be in continuation of the subarray.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n    long long solve(vector<int>& nums, int i, int state, int minus, vector <vector <vector <long long>>> &dp) {\n\n        if(i == nums.size()) return 0;\n\n        int x = minus == -1 ? 0: 1;\n\n        if(dp[i][state][x] != -1) {\n\n            return dp[i][state][x];\n\n        }\n\n        long long curr_array = -1e9, end_array = -1e9;\n\n        if(state == 1) {\n\n            curr_array = nums[i]*minus + solve(nums, i+1, 1, minus*-1, dp);\n            end_array = nums[i]*minus + solve(nums, i+1, 0, 1, dp);\n\n        }\n\n        else {\n\n            curr_array = nums[i] + solve(nums, i+1, 1, -1, dp);\n            end_array = nums[i] + solve(nums, i+1, 0, -1, dp);\n\n        }\n\n        return dp[i][state][x] = max(curr_array, end_array);\n    }\n\n\n    long long maximumTotalCost(vector<int>& nums) {\n\n        vector <vector <vector <long long>>> dp(nums.size(), vector <vector <long long>> (2, vector <long long> (2, -1)));\n        \n        return solve(nums, 0, 0, 1, dp);\n    }\n};\n```
['C++']