🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯
Intuition\n\n\n\njava []\nclass Solution {\n    public long maximumValueSum(int[] A, int k, int[][] edges) {\n        long res = 0;\n        int d = 1 << 30, c 
# Intuition\n![Screenshot 2024-03-02 220117.png](https://assets.leetcode.com/users/images/89e4126d-2285-43b9-b083-9384f61f9f1c_1709397263.9189143.png)\n![Screenshot 2024-03-02 220022.png](https://assets.leetcode.com/users/images/03bd9dd3-802f-4c47-8360-a6ae7b1dc050_1709397268.9936154.png)\n![Screenshot 2024-03-02 215703.png](https://assets.leetcode.com/users/images/7c952bc4-6953-42d1-bd45-a905244de94b_1709397275.3138962.png)\n```java []\nclass Solution {\n    public long maximumValueSum(int[] A, int k, int[][] edges) {\n        long res = 0;\n        int d = 1 << 30, c = 0;\n        for (int a : A) {\n            int b = a ^ k;\n            res += Math.max(a, b);\n            c ^= a < b ? 1 : 0;\n            d = Math.min(d, Math.abs(a - b));\n        }\n        return res - d * c;\n    }\n}\n```\n```python3 []\nclass Solution:\n    def maximumValueSum(self, A: List[int], k: int, edges: List[List[int]]) -> int:\n        res = c = 0\n        d = 1 << 30\n        for a in A:\n            res += max(a, b := a ^ k)\n            c ^= a < b\n            d = min(d, abs(a - b))\n        return res - d * c\n```\n```C++ []\nclass Solution {\npublic:\n    long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n        int cnt = 0, Min = 2e9;\n        long long ans = 0;\n        for (auto it : nums) {\n            int x = it;\n            int y = it ^ k;\n            if (x > y)\n                ans += x;\n            else\n                ans += y, cnt++;\n            Min = min(Min, abs(x - y));\n        }\n        if (cnt % 2 == 1)\n            ans -= Min;\n        return ans;\n    }\n};\n```\n```python []\nclass Solution(object):\n    def maximumValueSum(self, nums, k, edges):\n        """\n        :type nums: List[int]\n        :type k: int\n        :type edges: List[List[int]]\n        :rtype: int\n        """\n        n, cnt, min_diff = len(nums), 0, float(\'inf\')\n        for i, v in enumerate(nums):\n            if v ^ k > v:\n                nums[i] ^= k\n                cnt += 1\n            min_diff = min(min_diff, nums[i] - (nums[i] ^ k))\n        return sum(nums) - (min_diff if cnt & 1 else 0)\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n   O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n   O(1)\n# Code\n```\nclass Solution:\n    def maximumValueSum(self, A: List[int], k: int, edges: List[List[int]]) -> int:\n        res = c = 0\n        d = 1 << 30\n        for a in A:\n            res += max(a, b := a ^ k)\n            c ^= a < b\n            d = min(d, abs(a - b))\n        return res - d * c\n\n```\n![th.jpeg](https://assets.leetcode.com/users/images/54ce4195-56c6-46ef-af08-8a245779502f_1709397353.6306124.jpeg)\n
['Array', 'Dynamic Programming', 'Greedy', 'Bit Manipulation', 'Tree', 'C', 'Python', 'C++', 'Java', 'Python3']