Most Optimized O(n) time and O(1) Space
Intuition\nThe last subarray can end with subtraction or addition, If the last subarray was addition your option for the next element is to either continue that
# Intuition\nThe last subarray can end with subtraction or addition, If the last subarray was addition your option for the next element is to either continue that subarray with a subtraction, OR start a new subarray with addition. \n\nIf the last subarray was a subtraction, you only have one option, that you can only add the current element, this can be considered as the start of the new subarray. \n\n\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```python3 []\nclass Solution:\n    def maximumTotalCost(self,nums):\n        n = len(nums)\n        a,b = nums[0], -float("inf")\n        for i in range(1,n):\n            c = max( b + nums[i], a + nums[i])\n            d = a - nums[i]\n            a , b = c, d\n        return max(a,b)\n\n```
['Python3']