TC : O(n), SC: O(1) DP
\n# Code\ntypescript []\nfunction maximumTotalCost(nums: number[]): number {\n    // we only take positive this mean we are startign a new sub array from here\n
\n# Code\n```typescript []\nfunction maximumTotalCost(nums: number[]): number {\n    // we only take positive this mean we are startign a new sub array from here\n    let n = nums.length;\n    if(n === 1)return nums[0];\n    let maxYet = nums[0];\n    let posPrev = 0, posCurr = 0, negPrev = 0, negCurr = 0;\n    for(let i = 0; i < n; i++)\n    {\n        posCurr = nums[i] + (i === 0 ? 0 : maxYet)\n        if(i > 0)\n        {\n            negCurr = posPrev - nums[i];\n            maxYet = Math.max(posCurr, negCurr)\n        }\n        posPrev = posCurr;\n        negPrev = negCurr;\n    }\n    return Math.max(posCurr, negCurr)\n};\n```
['TypeScript']