Solution By Dare2Solve | Detailed Explanation | 1D DP
Explanation []\nauthorslog.vercel.app/blog/fwKyrRoXX5\n\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    long long maximumTotalCost(vector<int>& nums) {\n   
```Explanation []\nauthorslog.vercel.app/blog/fwKyrRoXX5\n```\n\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    long long maximumTotalCost(vector<int>& nums) {\n        int n = nums.size();\n        vector<long long> dp(n + 1, -LLONG_MAX);\n        dp[0] = 0;\n        dp[1] = nums[0];\n        for (int i = 1; i < n; ++i) {\n            dp[i + 1] = max(dp[i] + nums[i], dp[i - 1] + nums[i - 1] - nums[i]);\n        }\n        return dp[n];\n    }\n};\n```\n\n```Python []\nclass Solution:\n    def maximumTotalCost(self, nums):\n        n = len(nums)\n        dp = [0] * (n + 1)\n        dp[1] = nums[0]\n        for i in range(1, n):\n            dp[i + 1] = max(dp[i] + nums[i], dp[i - 1] + nums[i - 1] - nums[i])\n        return dp[n]\n```\n\n```java []\nclass Solution {\n    public long maximumTotalCost(int[] nums) {\n        int n = nums.length;\n        long[] dp = new long[n + 1];\n        dp[0] = 0;\n        dp[1] = nums[0];\n        for (int i = 1; i < n; ++i) {\n            dp[i + 1] = Math.max(dp[i] + nums[i], dp[i - 1] + nums[i - 1] - nums[i]);\n        }\n        return dp[n];\n    }\n}\n```\n\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumTotalCost = function (nums) {\n  var dp = new Array(nums.length + 1);\n  dp[0] = 0;\n  dp[1] = nums[0];\n  for (var i = 1; i < nums.length; ++i) {\n    dp[i + 1] = Math.max(dp[i] + nums[i], dp[i - 1] + nums[i - 1] - nums[i]);\n  }\n  return dp[nums.length];\n};\n```\n\n```Go []\nfunc maximumTotalCost(nums []int) int64 {\n\tn := len(nums)\n\tdp := make([]int64, n+1)\n\tdp[1] = int64(nums[0])\n\tfor i := 1; i < n; i++ {\n\t\tdp[i+1] = max(dp[i]+int64(nums[i]), dp[i-1]+int64(nums[i-1])-int64(nums[i]))\n\t}\n\treturn dp[n]\n}\n\nfunc max(a, b int64) int64 {\n\tif a > b {\n\t\treturn a\n\t}\n\treturn b\n}\n```\n\n```csharp []\npublic class Solution {\n    public long MaximumTotalCost(int[] nums) {\n        int n = nums.Length;\n        long[] dp = new long[n + 1];\n        dp[0] = 0;\n        dp[1] = nums[0];\n        for (int i = 1; i < n; ++i) {\n            dp[i + 1] = Math.Max(dp[i] + nums[i], dp[i - 1] + nums[i - 1] - nums[i]);\n        }\n        return dp[n];\n    }\n}\n```
['Dynamic Programming', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']