JAVA - 100% Faster - (2D) DP - (Positive - Negative) Approach
\n# Code\n\nclass Solution {\n    public long maximumTotalCost(int[] nums) {\n        \n        int n = nums.length;\n        long add =0, sub=0;\n        if(n=
\n# Code\n```\nclass Solution {\n    public long maximumTotalCost(int[] nums) {\n        \n        int n = nums.length;\n        long add =0, sub=0;\n        if(n==1){\n            return nums[0];\n        }\n        // long[][] dp = new long[n][2];\n        \n        // dp[0][0] = nums[0];\n        // dp[0][1] = nums[0];\n        add = nums[0];\n        sub = nums[0];\n\n        for(int i=1;i<n;i++){\n            long temp = Math.max(add, sub) +nums[i];\n            sub = add - nums[i];\n            add = temp;\n        }\n\n        return Math.max(add,sub);\n    }\n}\n```
['Java']