🔥✅ | 99.78% beats ✅  | ✅ Easy Code | ✅🔥
\n# Intuition and Approach\nThe goal of this solution is to maximize the sum of values of nodes in a tree after possibly performing XOR operations with a given 
![Screenshot from 2024-05-19 11-31-23.png](https://assets.leetcode.com/users/images/fe4de8bb-9f71-47db-83cc-b15fee7f48b5_1716098539.8577147.png)\n# Intuition and Approach\nThe goal of this solution is to maximize the sum of values of nodes in a tree after possibly performing XOR operations with a given integer k on the nodes any number of times. Here\'s the intuition and approach behind the code:\n\n# Initial Sum Calculation:\n\n- Iterate through the list of node values (nums).\n- For each node value, calculate its XOR with k (X = num ^ k).\n- If the XOR-ed value (X) is greater than the original value (num), include X in the sum and increase a counter (cnt) to keep track of how many times the XOR operation was beneficial.\n- If the XOR-ed value is not greater, include the original value in the sum.\n# Adjust for Even Count:\n\n- If the number of beneficial XOR operations (cnt) is even, the result is directly the computed sum.\n- If the number of beneficial XOR operations is odd, subtract the smallest difference between the XOR-ed value and the original value (minDiff). This adjustment ensures that the sum is maximized while keeping the beneficial operations count even, which is essential because each XOR operation can be reversed by applying it again.\n# Complexity:\n\n- Time Complexity: O(n), where n is the number of nodes. This is because we iterate through the node values once to compute the sum and the beneficial XOR operations.\n- Space Complexity: O(1), as we use a few additional variables but no extra space that scales with input size.\n# Code\n```C++ []\nclass Solution {\npublic:\n    long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n        ios::sync_with_stdio(false);\n        cin.tie(NULL);\n        cout.tie(NULL);\n\n        long long sum = 0;\n        int cnt=0,minDiff = INT_MAX;\n        for(const auto & num : nums){\n            int X = (num^k);\n            if(X > num)\n            {\n                sum += X;\n                cnt++;\n            }\n            else\n            {\n                sum += num;\n            }\n            minDiff = min(minDiff,abs(X-num));\n        }\n        if(cnt&1){\n            return (sum - minDiff);\n        }\n        return sum;\n    }\n};\n```\n```Java []\n\npublic class Solution {\n    public long maximumValueSum(int[] nums, int k, List<int[]> edges) {\n        long sum = 0;\n        int cnt = 0;\n        int minDiff = Integer.MAX_VALUE;\n\n        for (int num : nums) {\n            int X = num ^ k;\n            if (X > num) {\n                sum += X;\n                cnt++;\n            } else {\n                sum += num;\n            }\n            minDiff = Math.min(minDiff, Math.abs(X - num));\n        }\n\n        if ((cnt & 1) == 1) {\n            return sum - minDiff;\n        }\n        return sum;\n    }\n}\n\n```\n```Python []\nclass Solution:\n    def maximumValueSum(self, nums, k, edges):\n        sum = 0\n        cnt = 0\n        minDiff = float(\'inf\')\n\n        for num in nums:\n            X = num ^ k\n            if X > num:\n                sum += X\n                cnt += 1\n            else:\n                sum += num\n            minDiff = min(minDiff, abs(X - num))\n\n        if cnt % 2 == 1:\n            return sum - minDiff\n        return sum\n```\n```Javascript []\nclass Solution {\n    maximumValueSum(nums, k, edges) {\n        let sum = 0;\n        let cnt = 0;\n        let minDiff = Infinity;\n\n        for (const num of nums) {\n            const X = num ^ k;\n            if (X > num) {\n                sum += X;\n                cnt++;\n            } else {\n                sum += num;\n            }\n            minDiff = Math.min(minDiff, Math.abs(X - num));\n        }\n\n        if (cnt % 2 === 1) {\n            return sum - minDiff;\n        }\n        return sum;\n    }\n}\n```\n\n\n![](https://mir-s3-cdn-cf.behance.net/project_modules/1400/e0bcc865344605.5f62f95707bd4.jpg)
['Array', 'Greedy', 'Bit Manipulation', 'Tree', 'Python', 'C++', 'Java', 'JavaScript']