Very concise and simple O(n) time/O(1) Space Solution
Intuition\nNo matter what happened before any num x, we can always add x to our running total. Conversely, we can only subtract x, if we added the previous num.
# Intuition\nNo matter what happened before any num `x`, we can always *add* `x` to our running total. Conversely, we can only *subtract* `x`, if we added the previous num.\nSo if we can keep track of the maximum possible cost for the cases when we *add* and when we *subtract* the *last* num, we can then calculate the maximum possible cost for the current num.\n\n# Approach\nWe use a size-2 array `dp` to keep track of the two possible maximum cost states as described above and iterate over all of `nums`. Our answer at the end is going to be the `max` over `dp`.\n\n# Complexity\n- Time complexity: O(`|nums.size|`)\n\n- Space complexity: O(`1`)\n\n# Kotlin\n```Kotlin\nclass Solution {\n    fun maximumTotalCost(nums: IntArray): Long {\n        val dp = LongArray(2) { nums[0].toLong() }\n        for (x in nums.drop(1)) {\n            val plus = dp.max() + x\n            val minus = dp[0] - x\n            dp[0] = plus\n            dp[1] = minus\n        }\n\n        return dp.max()\n    }\n}\n```
['Dynamic Programming', 'Kotlin']