Faster⏰||Simpler🧠||Efficient✅
\n# Code\npython3 []\nclass Solution:\n    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:\n        totalSum = 0\n        cou
\n# Code\n```python3 []\nclass Solution:\n    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:\n        totalSum = 0\n        count = 0\n        positiveMin = float("inf")\n        negativeMax = float("-inf")\n\n        for nodeValue in nums:\n            nodeValAfterOperation = nodeValue ^ k\n\n            totalSum += nodeValue\n            netChange = nodeValAfterOperation - nodeValue\n\n            if netChange > 0:\n                positiveMin = min(positiveMin, netChange)\n                totalSum += netChange\n                count += 1\n            else:\n                negativeMax = max(negativeMax, netChange)\n\n        if count % 2 == 0:\n            return totalSum\n        return max(totalSum - positiveMin, totalSum + negativeMax)\n```\n```C++ []\nclass Solution {\npublic:\n    long long maximumValueSum(vector<int>& nums, int k,\n                              vector<vector<int>>& edges) {\n        long long totalSum = 0;\n        int count = 0;\n        int positiveMin = INT_MAX;\n        int negativeMax = INT_MIN;\n\n        for (int nodeValue : nums) {\n            int nodeValAfterOperation = nodeValue ^ k;\n            totalSum += nodeValue;\n            int netChange = nodeValAfterOperation - nodeValue;\n\n            if (netChange > 0) {\n                positiveMin = min(positiveMin, netChange);\n                totalSum += netChange;\n                count += 1;\n            } else {\n                negativeMax = max(negativeMax, netChange);\n            }\n        }\n\n        if (count % 2 == 0) {\n            return totalSum;\n        }\n        return max(totalSum - positiveMin, totalSum + negativeMax);\n    }\n};\n```\n```Java []\nclass Solution {\n    public long maximumValueSum(int[] nums, int k, int[][] edges) {\n        long totalSum = 0;\n        int count = 0;\n        int positiveMin = Integer.MAX_VALUE;\n        int negativeMax = Integer.MIN_VALUE;\n\n        for (int nodeValue : nums) {\n            int nodeValAfterOperation = nodeValue ^ k;\n            totalSum += nodeValue;\n            int netChange = nodeValAfterOperation - nodeValue;\n\n            if (netChange > 0) {\n                positiveMin = Math.min(positiveMin, netChange);\n                totalSum += netChange;\n                count += 1;\n            } else {\n                negativeMax = Math.max(negativeMax, netChange);\n            }\n        }\n\n        if (count % 2 == 0) {\n            return totalSum;\n        }\n        return Math.max(totalSum - positiveMin, totalSum + negativeMax);\n    }\n}\n```\n``` JavaScript []\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number[][]} edges\n * @return {number}\n */\nvar maximumValueSum = function (nums, k, edges) {\n    let totalSum = 0;\n    let count = 0;\n    let positiveMin = Infinity;\n    let negativeMax = -Infinity;\n\n    for (let nodeValue of nums) {\n        let nodeValAfterOperation = nodeValue ^ k;\n        totalSum += nodeValue;\n        let netChange = nodeValAfterOperation - nodeValue;\n\n        if (netChange > 0) {\n            positiveMin = Math.min(positiveMin, netChange);\n            totalSum += netChange;\n            count += 1;\n        } else {\n            negativeMax = Math.max(negativeMax, netChange);\n        }\n    }\n\n    if (count % 2 === 0) {\n        return totalSum;\n    }\n    return Math.max(totalSum - positiveMin, totalSum + negativeMax);\n};\n```
['C++', 'Java', 'Python3', 'JavaScript']