#3068
Find the Maximum Sum of Node Values
expert · 1015 · lc hard +32 · verified · 69.5% accepted · 919 likes · top 77%
Description
An undirected tree has n nodes (0 to n - 1) with edges edges[i] = [ui, vi], a positive integer k, and node values nums[i]. Alice wants to maximize the total node value sum. Any number of times she may select an edge [u, v] and XOR both endpoints with k:
- nums[u] = nums[u] XOR k
- nums[v] = nums[v] XOR k
Return the maximum achievable sum of all node values.
Example 1:
Input: nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
Output: 6
Explanation: Alice can achieve the maximum sum of 6 using a single operation:
- Choose the edge [0,2]. nums[0] and nums[2] become: 1 XOR 3 = 2, and the array nums becomes: [1,2,1] -> [2,2,2].
The total sum of values is 2 + 2 + 2 = 6.
It can be shown that 6 is the maximum achievable sum of values.
Example 2:
Input: nums = [2,3], k = 7, edges = [[0,1]]
Output: 9
Explanation: Alice can achieve the maximum sum of 9 using a single operation:
- Choose the edge [0,1]. nums[0] becomes: 2 XOR 7 = 5 and nums[1] become: 3 XOR 7 = 4, and the array nums becomes: [2,3] -> [5,4].
The total sum of values is 5 + 4 = 9.
It can be shown that 9 is the maximum achievable sum of values.
Example 3:
Input: nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
Output: 42
Explanation: The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
Code
1
2
3