Easy To Understand
Intuition\n Describe your first thoughts on how to solve this problem. \n- I will try to XOR every element such that the sum increases.\n- XORing an element twi
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- I will try to XOR every element such that the sum increases.\n- XORing an element twice will not affect the sum.\n- I can choose any two nodes to apply XOR regardless of whether there is an edge between them or not, as reaching that node will result in every intermediate node being XORed twice, which will not affect the intermediate nodes.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Iterate over all nodes in nums, and if XORing an element increases its value, include that node in sum1 and increase the counter of operations.\n- Otherwise, include it in sum2, meaning no operation is done on it.\n- If the count is even, meaning we have chosen 2 nodes in every operation, we can directly return sum1 + sum2.\n- If the count is odd, meaning we have an extra node to remove from our operation or an extra node to add to our operation.\n- As mentioned above, we will do that for every node and store values in ans1 and ans2.\n- Finally, we will return the maximum of both.\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n        long sum1=0,sum2=0;\n        int cnt=0;\n        for(long long it:nums){\n            if((it^k)>it){\n                sum1+=(it^k);\n                cnt++;\n            }\n            else{\n                sum2+=it;\n            }\n        }\n        if(cnt%2==0){\n            return sum1+sum2;\n        }\n        long long ans1=0,ans2=0;\n        for(long long it:nums){\n            if((it^k)>it) ans1=max(ans1,sum1+sum2-(it^k)+it);\n            else ans2=max(ans2,sum1+sum2+(it^k)-it);\n        }\n        return max(ans1,ans2);\n    }\n};\n```
['Dynamic Programming', 'Greedy', 'Bit Manipulation', 'Tree', 'C++']