Greedy Solution with Explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nwhenever a new_element is added in front of a subarray the sum of subarray(prev) become
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nwhenever a **new_element** is added in front of a subarray the sum of subarray(**prev**) becomes (**new_element**) - (**prev**) lets call it **temp**. \nif the **prev** is positive, solution maximizes if we split, \n**temp = new_element + prev**.\notherwise the new_element is added in subarray and \n**temp = new_element - prev**.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIterating Backwards, if **prev >= 0**, we split, \n**ans += prev** and update the prev with **prev = nums[i]**,\nelse only update the prev, **prev = nums[i]-prev**.\nAt the end return **ans + prev**.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumTotalCost(vector<int>& nums) {\n        long long prev = 0, ans = 0;\n        for(int i = nums.size()-1; i>=0; i--){\n            if(prev >= 0){\n                ans += prev;\n                prev = nums[i];\n            }\n            else{\n                prev = nums[i]-prev;\n            }\n        }\n        return ans + prev;\n    }\n};\n```
['C++']