Fastest execution || Beats 100% users with java & typescript || python3 || C++ || Greedy
Approach\nGreedy\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Explanation\n1. Class Definition:\n\n- We define a class Solution whi
# Approach\nGreedy\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n# Explanation\n1. **Class Definition:**\n\n- We define a class Solution which contains the method maximumValueSum.\n2. **Method Signature:**\n\n- **The method maximumValueSum takes three parameters:**\n    - int[] nums: An array of integers representing the values of the nodes.\n    - int k: An integer to be used for XOR operations.\n    - int[][] edges: A 2D array representing the edges of the tree (though it\'s not actually used in the computation).\n3. **Variables Initialization:**\n\n- bestSum (of type long): This will store the best possible sum we can achieve.\n- cnt (of type int): This will count how many times the value of a node increases when XORed with k.\n4. **Loop through nums:**\n\n- **For each num in nums:**\n    - Calculate the maximum of the original value num and the value after XORing with k (num ^ k). Add this maximum value to bestSum.\n    - Check if XORing num with k results in a larger value than num. If it does, increment the cnt counter.\n5. **Adjusting the Sum for Odd cnt:**\n\n- If cnt is odd, it means that we have an imbalance in the number of nodes that benefit from XORing with k.\n- To balance it, we need to subtract the smallest possible difference between a node\'s original value and its XORed value from bestSum.\n- We initialize minDifference to the maximum possible integer value (Integer.MAX_VALUE).\n- Loop through each num in nums to find the minimum difference Math.abs(num - (num ^ k)).\n- Subtract this minDifference from bestSum.\n6. **Return the Result:**\n\n- Finally, return the calculated bestSum.\n\n---\n\n```java []\nclass Solution {\n    public long maximumValueSum(int[] nums, int k, int[][] edges) {\n        long bestSum = 0;\n        int cnt = 0;\n        \n        // Compute the best possible sum and count the number of nodes where XORing increases the value\n        for (int num : nums) {\n            bestSum += Math.max(num, num ^ k);\n            if ((num ^ k) > num) {\n                cnt++;\n            }\n        }\n        \n        // If cnt is odd, we need to adjust the sum\n        if (cnt % 2 != 0) {\n            int minDifference = Integer.MAX_VALUE;\n            for (int num : nums) {\n                minDifference = Math.min(minDifference, Math.abs(num - (num ^ k)));\n            }\n            bestSum -= minDifference;\n        }\n        \n        return bestSum;\n    }\n}\n\n```\n```python3 []\nclass Solution:\n    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:\n        best_sum = sum(max(n, k ^ n) for n in nums)\n        cnt = sum((n ^ k) > n for n in nums)  \n        return best_sum - (min(abs(n - (n ^ k)) for n in nums) if cnt % 2 else 0)\n```\n```C++ []\n#include <vector>\n#include <algorithm>\n#include <cmath>\n#include <numeric>\nusing namespace std;\n\nclass Solution {\npublic:\n    long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n        long long bestSum = 0;\n        int cnt = 0;\n\n        // Compute the best possible sum and count the number of nodes where XORing increases the value\n        for (int num : nums) {\n            bestSum += max(num, num ^ k);\n            if ((num ^ k) > num) {\n                cnt++;\n            }\n        }\n\n        // If cnt is odd, we need to adjust the sum\n        if (cnt % 2 != 0) {\n            int minDifference = INT_MAX;\n            for (int num : nums) {\n                minDifference = min(minDifference, abs(num - (num ^ k)));\n            }\n            bestSum -= minDifference;\n        }\n\n        return bestSum;\n    }\n};\n\n```\n```Typescript []\nfunction maximumValueSum(nums: number[], k: number, edges: number[][]): number {\n    let bestSum: number = 0;\n    let cnt: number = 0;\n\n    // Compute the best possible sum and count the number of nodes where XORing increases the value\n    for (let num of nums) {\n        bestSum += Math.max(num, num ^ k);\n        if ((num ^ k) > num) {\n            cnt++;\n        }\n    }\n\n    // If cnt is odd, we need to adjust the sum\n    if (cnt % 2 !== 0) {\n        let minDifference: number = Number.MAX_SAFE_INTEGER;\n        for (let num of nums) {\n            minDifference = Math.min(minDifference, Math.abs(num - (num ^ k)));\n        }\n        bestSum -= minDifference;\n    }\n\n    return bestSum;\n}\n```\n
['Array', 'Dynamic Programming', 'Greedy', 'Tree', 'C++', 'Java', 'TypeScript', 'Python3']