Clean java solution | DP easy to understand
Code\n\nclass Solution {\n    Map<String, Long> memo;\n    public long maximumTotalCost(int[] nums) {\n        memo = new HashMap<>();\n        return dfs(nums,
# Code\n```\nclass Solution {\n    Map<String, Long> memo;\n    public long maximumTotalCost(int[] nums) {\n        memo = new HashMap<>();\n        return dfs(nums, 0, true);\n    }\n    private long dfs(int[] nums, int i, boolean sign){\n        if(i == nums.length)\n            return 0;\n\n        String state = i + "," + sign;\n        if(memo.containsKey(state))\n            return memo.get(state);\n\n        long res = nums[i] * (sign ? 1: -1);\n        res += Math.max(dfs(nums, i + 1, true), dfs(nums, i + 1, !sign));\n        memo.put(state, res);\n\n        return res;\n    }\n}\n```
['Java']