My O(N) time and O(1) space most optimal dp solution
\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n    long long maximumTotalCost(vector<int>& nums
\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumTotalCost(vector<int>& nums) {\n        int n = nums.size();\n        long long last0 = nums[0];\n        long long last1 = nums[0];\n        for (int ind = 1; ind < n; ind++) {\n            long long curr0 = max(abs(nums[ind]) + last1, \n            nums[ind] + last0);\n            long long curr1 = nums[ind] + last0;\n            last0 = curr0;\n            last1 = curr1;\n        }\n        return last0;\n    }\n};\n```
['C++']