🔥 BEATS 100% |✅ [  Java / C++ / Py / C / C# / JS / GO  ]
Intuition\nTo maximize the sum of values of tree nodes, we need to carefully choose which edges to update and which to leave unchanged. Since updating an edge a
# Intuition\nTo maximize the sum of values of tree nodes, we need to carefully choose which edges to update and which to leave unchanged. Since updating an edge affects the values of both nodes connected by that edge, we need to consider the effect of each update on the overall sum of values.\n\n# Approach\n1. Initialize variables `sum` to store the current sum of values, `minExtra` to track the minimum extra value gained from performing operations, and `count` to count the number of nodes whose values are updated by operations.\n2. Iterate through each node\'s value in the `nums` array.\n3. For each node\'s value `val`, if `(val ^ k) > val`, it means updating this node and its connected node with XOR operation will increase the sum. Update `sum` by adding `(val ^ k)` to it and update `minExtra` by taking the minimum of its current value and `(val ^ k) - val`. Increment `count`.\n4. If `(val ^ k) <= val`, it means updating this node and its connected node won\'t increase the sum. Update `sum` by adding `val` to it and update `minExtra` by taking the minimum of its current value and `val - (val ^ k)`.\n5. After iterating through all nodes, if `count` is even, return the current `sum`, else return `sum - minExtra`.\n\n# Complexity\n- Time complexity: $$O(n)$$, where $$n$$ is the number of nodes in the tree.\n- Space complexity: $$O(1)$$\n\n```Java []\nclass Solution {\n    public long maximumValueSum(int[] nums, int k, int[][] edges) {  \n        long sum = 0;\n        long minExtra = 1000000;\n        int count = 0;\n\n        for( int val : nums) {\n            if ((val ^ k) > val ) {\n                sum += val ^ k;\n                minExtra = Math.min(minExtra, (val ^ k)- val);\n                count++;\n            } else {\n                sum += val;\n                minExtra = Math.min(minExtra, val - (val ^ k));\n            }\n        }\n\n        if ( count %2 ==0 ) {\n            return sum;\n        } else {\n            return sum - minExtra;\n        }\n\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    long long maximumValueSum(std::vector<int>& nums, int k, std::vector<std::vector<int>>& edges) {\n        long long sum = 0;\n        long long minExtra = 1000000;\n        int count = 0;\n\n        for (int val : nums) {\n            if ((val ^ k) > val) {\n                sum += (val ^ k);\n                minExtra = std::min(minExtra, static_cast<long long>((val ^ k) - val));\n                count++;\n            } else {\n                sum += val;\n                minExtra = std::min(minExtra, static_cast<long long>(val - (val ^ k)));\n            }\n        }\n\n        if (count % 2 == 0) {\n            return sum;\n        } else {\n            return sum - minExtra;\n        }\n    }\n};\n\n```\n```python []\nclass Solution:\n    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:\n        sum_ = 0\n        min_extra = 1000000\n        count = 0\n\n        for val in nums:\n            if (val ^ k) > val:\n                sum_ += val ^ k\n                min_extra = min(min_extra, (val ^ k) - val)\n                count += 1\n            else:\n                sum_ += val\n                min_extra = min(min_extra, val - (val ^ k))\n\n        if count % 2 == 0:\n            return sum_\n        else:\n            return sum_ - min_extra\n\n```\n```C []\nlong long maximumValueSum(int* nums, int numsSize, int k, int** edges, int edgesSize, int* edgesColSize) {\n    long long sum = 0;\n    long long minExtra = 1000000;  // Large initial value for minExtra\n    int count = 0;\n\n    for (int i = 0; i < numsSize; i++) {\n        if ((nums[i] ^ k) > nums[i]) {\n            sum += nums[i] ^ k;\n            minExtra = (minExtra < ((nums[i] ^ k) - nums[i])) ? minExtra : ((nums[i] ^ k) - nums[i]);\n            count++;\n        } else {\n            sum += nums[i];\n            minExtra = (minExtra < (nums[i] - (nums[i] ^ k))) ? minExtra : (nums[i] - (nums[i] ^ k));\n        }\n    }\n\n    if (count % 2 == 0) {\n        return sum;\n    } else {\n        return sum - minExtra;\n    }\n}\n\n```\n```C# []\npublic class Solution {\n    public long MaximumValueSum(int[] nums, int k, int[][] edges) {\n        long sum = 0;\n        long minExtra = 1000000; \n        int count = 0;\n\n        foreach (int val in nums) {\n            if ((val ^ k) > val) {\n                sum += val ^ k;\n                minExtra = Math.Min(minExtra, (val ^ k) - val);\n                count++;\n            } else {\n                sum += val;\n                minExtra = Math.Min(minExtra, val - (val ^ k));\n            }\n        }\n\n        return (count % 2 == 0) ? sum : sum - minExtra;\n    }\n}\n\n```\n```javascript []\nvar maximumValueSum = function(nums, k, edges) {\n    let sum = 0;\n    let minExtra = 1000000; \n    let count = 0;\n\n    for (const val of nums) {\n        if ((val ^ k) > val) {\n            sum += val ^ k;\n            minExtra = Math.min(minExtra, (val ^ k) - val);\n            count++;\n        } else {\n            sum += val;\n            minExtra = Math.min(minExtra, val - (val ^ k));\n        }\n    }\n\n    return count % 2 === 0 ? sum : sum - minExtra;\n};\n\n```\n```Go []\nfunc maximumValueSum(nums []int, k int, edges [][]int) int64 {\n    var sum int64 = 0\n    var minExtra int64 = 1000000 \n    count := 0\n\n    for _, val := range nums {\n        if (val ^ k) > val {\n            sum += int64(val ^ k)\n            minExtra = min(minExtra, int64((val ^ k) - val))\n            count++\n        } else {\n            sum += int64(val)\n            minExtra = min(minExtra, int64(val - (val ^ k)))\n        }\n    }\n\n    if count % 2 == 0 {\n        return sum\n    } else {\n        return sum - minExtra\n    }\n}\n\nfunc min(a, b int64) int64 {\n    if a < b {\n        return a\n    }\n    return b\n}\n\n```\n**NOTE: THE FOLLOWING IMPLEMENTATION BEATS 100% IN JAVA .. DON\'T KNOW ABOUT OTHER CODES** \n```\nUPVOTE\u2B06\uFE0F IF IT HELPED\uD83D\uDE0A\n```\n
['C', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']