DP+Space optimized .....||Beats 99.90% ✅✅
\n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nGoal was to maximise the total cost where cost is computed using alternative sum.
![Screenshot 2024-08-01 at 12.07.09\u202FAM.png](https://assets.leetcode.com/users/images/64613915-6b4e-4234-82e3-d7ffd6b96874_1722451061.304263.png)\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nGoal was to maximise the total cost where cost is computed using alternative sum...use dp to track the sum.For space optimization instead of using 2D dp use variables to store previous result/state.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumTotalCost(vector<int>& nums) \n    {\n        ios_base::sync_with_stdio(false); \n        cin.tie(NULL);\n        int n = nums.size();\n        if (n == 1) \n        {\n            return nums[0];\n        }\n         // Initialize variables for dp[i-1]\n        long long prev0 = nums[0], prev1 = LLONG_MIN;\n        long long curr0, curr1;\n\n        for (int i = 1; i < n; i++) \n        {\n            curr0 = max(prev0, prev1) + nums[i];\n            curr1 = prev0 - nums[i];\n            \n            prev0 = curr0;\n            prev1 = curr1;\n        }\n\n        return max(prev0, prev1);\n    }\n};\n\n\nUpvote if you find it easy to analyse??!!\n```
['C++']