<p>There exists an <strong>undirected</strong> tree with <code>n</code> nodes numbered <code>0</code> to <code>n - 1</code>. You are given a <strong>0-indexed</strong> 2D integer array <code>edges</code> of length <code>n - 1</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> indicates that there is an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code> in the tree. You are also given a <strong>positive</strong> integer <code>k</code>, and a <strong>0-indexed</strong> array of <strong>non-negative</strong> integers <code>nums</code> of length <code>n</code>, where <code>nums[i]</code> represents the <strong>value</strong> of the node numbered <code>i</code>.</p>

<p>Alice wants the sum of values of tree nodes to be <strong>maximum</strong>, for which Alice can perform the following operation <strong>any</strong> number of times (<strong>including zero</strong>) on the tree:</p>

<ul>
	<li>Choose any edge <code>[u, v]</code> connecting the nodes <code>u</code> and <code>v</code>, and update their values as follows:

	<ul>
		<li><code>nums[u] = nums[u] XOR k</code></li>
		<li><code>nums[v] = nums[v] XOR k</code></li>
	</ul>
	</li>
</ul>

<p>Return <em>the <strong>maximum</strong> possible <strong>sum</strong> of the <strong>values</strong> Alice can achieve by performing the operation <strong>any</strong> number of times</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012513.png" style="width: 300px; height: 277px;padding: 10px; background: #fff; border-radius: .5rem;" />
<pre>
<strong>Input:</strong> nums = [1,2,1], k = 3, edges = [[0,1],[0,2]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 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] -&gt; [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.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/09/screenshot-2024-01-09-220017.png" style="padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem; width: 300px; height: 239px;" />
<pre>
<strong>Input:</strong> nums = [2,3], k = 7, edges = [[0,1]]
<strong>Output:</strong> 9
<strong>Explanation:</strong> 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] -&gt; [5,4].
The total sum of values is 5 + 4 = 9.
It can be shown that 9 is the maximum achievable sum of values.
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/11/09/screenshot-2023-11-10-012641.png" style="width: 600px; height: 233px;padding: 10px; background: #fff; border-radius: .5rem;" />
<pre>
<strong>Input:</strong> nums = [7,7,7,7,7,7], k = 3, edges = [[0,1],[0,2],[0,3],[0,4],[0,5]]
<strong>Output:</strong> 42
<strong>Explanation:</strong> The maximum achievable sum is 42 which can be achieved by Alice performing no operations.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= n == nums.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>edges.length == n - 1</code></li>
	<li><code>edges[i].length == 2</code></li>
	<li><code>0 &lt;= edges[i][0], edges[i][1] &lt;= n - 1</code></li>
	<li>The input is generated such that <code>edges</code> represent&nbsp;a valid tree.</li>
</ul>

class Solution:
    # what is the range of values len(nums) can take on? [2, 20000]
    # what is the range of values nums[i] can take on? [0, 1 billion]
    # what is the range of values k can take on? [1, 1 billion]
    # what is the range of values len(edges) can take on? exactly n-1? yes
    # what is the range of values len(edges[i]) can take on? exactly 2
    # what is the range of values edges[i][j] can take on? [0, n-1]
    # are there duplicates in edges? no
    # are there duplicates in nums? yes
    # can an edge connect a node to itself? no
    # edges are UNDIRECTED!
    # the graph is a TREE!
    # can nums array be a null reference? no
    # can edges array be a null reference? no
    # can edges[i] be a null reference? no
    # can we mutate nums array? no?
    # can we mutate edges array? no?
    # can we mutate edges[i]? no?
    # we are returning a maximized integer!
    # is nums sorted in any particular order? no
    # is edges sorted in any particular order? no
    # are there any float values in the input? no
    # is the answer unique? yes
    # is the answer >=0? yes

    # idea 0
    # dyanmic programming for a maximization problem?
    # use a 2 dimensional cache to store the maximized
    #    XOR between any two numbers x and y
    # iterate thru each edge in the tree
    # repeatedly apply the operation
    #    and maximize the sum of nums[i] and nums[j]
    #    until maximized
    #    then cache the maximized value in dp(origValueOfI,origValueOfJ)
    # want to keep iterating thru edges until nums
    #    hasn't been updated at all
    # dp = {(1,2):W, (0,2)}
    # 0 - 1: (1,2)
    # 0 - 2: (1,1)
    # maximized
    # pseudocode
    # ans = float("-inf")
    # dp_cache = {}
    # graph = {}
    # for idx,nodeVal in enumerate(nums):
    #    graph[idx] = nodeVal
    # updated = True
    # while updated:
    #     updated=False
    #     for a,b in edges:
    #        res = float("-inf")
    #        val_a,val_b = graph[a],graph[b]
    #        if (val_a,val_b) in dp_cache:
    #            res= dp_cache[(val_a,val_b)]
    #        elif (val_b,val_a) in dp_cache:
    #            res= dp_cache[(val_b,val_a)]
    #        else:
    #            res = val_a+val_b
    #            ans -= res
    #            while True:
    #                val_a^=k
    #                val_b^=k
    #                if val_a+val_b >= res:
    #                    res = val_a+val_b
    #                    updated=True
    #                else:
    #                    break
    #            dp_cache[(val_a,val_b)] = res
    #            dp_cache[(val_b,val_a)] = res
    #            ans+=res
    #   return ans
    #             

    def maximumValueSum(self, nums: List[int], k: int, edges: List[List[int]]) -> int:
        # implement idea 0 (DP + iterate thru edges) <<< close but no cigar
        # dp_cache = {}
        # graph = nums.copy()
        # updated = True
        # while updated:
        #     updated=False
        #     for a,b in edges:
        #         val_a,val_b = graph[a],graph[b]
        #         if (val_a,val_b) in dp_cache:
        #             val_a,val_b= dp_cache[(val_a,val_b)]
        #         elif (val_b,val_a) in dp_cache:
        #             val_b,val_a= dp_cache[(val_b,val_a)]
        #         else:
        #             a_,b_ = val_a,val_b
        #             while (val_a^k)+(val_b^k) > val_a+val_b:
        #                 val_a^=k
        #                 val_b^=k
        #                 updated=True
        #             # at this point, (val_a^k)+(val_b^k) <= val_a+val_b
        #             dp_cache[(a_,b_)] = (val_a,val_b)
        #             dp_cache[(b_,a_)] = (val_b,val_a)
        #         graph[a],graph[b] = val_a,val_b
        # return sum(graph)
        
        # LeetCode Editorial Approach 1 (bottom up memoized DP)
        # NOTE: edges is NOT used at all in this approach
        # n= len(nums)
        # memo = [[-1,-1] for _ in range(n)] # O(2N) = O(N) time
        # def dp(idx,isEven):
        #     if idx==n:
        #         return 0 if isEven else float("-inf")
        #     if memo[idx][isEven]!=-1:
        #         return memo[idx][isEven]
        #     # perform no operation
        #     noXOR = nums[idx]+dp(idx+1,isEven)
        #     # perform an operation
        #     yesXOR = (nums[idx]^k) + dp(idx+1,not isEven)
        #     memo[idx][isEven] = max(noXOR,yesXOR)
        #     return memo[idx][isEven]
        # return dp(0,True)
        # Time = O(2N+2N) = O(N) for the 2d memo array
        #    initialization and the populating of the memo
        #    cache
        # Memory = O(N + 2N) = O(N) for the max recursion depth
        #     of the dp() helper method and the 2d memo cache array

        # LeetCode Editorial Approach 2 (bottom up tabulated DP)
        # NOTE: see approach 1 above for more info on this approach
        n = len(nums)
        dp = [[0,0] for _ in range(n+1)]
        dp[n][1] = 0
        dp[n][0] = float("-inf")
        for idx in reversed(range(n)):
            for isEven in range(2):
                # case 1: yes xor
                yesXOR = dp[idx+1][isEven^1] + (nums[idx]^k)
                # case 2: no xor
                noXOR = dp[idx+1][isEven] + nums[idx]
                dp[idx][isEven] = max(yesXOR,noXOR)
        return dp[0][1]
1020