Tap here to switch tabs
Problem
Submissions

Problem: Find the Maximum Sum of Node Values

Try to solve the Find the Maximum Sum of Node Values problem.

Statement

There exists an undirected tree with n nodes labeled from 00 to n −1- 1. You are given a 00 indexed integer array nums of length n, where nums[i] represents the value of the iextthi^{ ext{th}} node. You are also given a positive integer k and a 2extD2 ext{D} integer array edges of length n −1- 1, where edges[i] == [u, v] indicates an undirected edge between nodes u and v in the tree.

You can perform the following operation any number of times (including zero times):

  • Choose an edge [u, v] connecting nodes u and v.

  • Update nums[u] to be nums[u] XOR k.

  • Update nums[v] to be nums[v] XOR k.

Return the maximum possible sum of the values of all nodes in the tree after applying the operation any number of times.

Constraints:

  • 2≤2 \leq n ==== nums.length ≤2imes104\leq 2 imes 10^4

  • 1≤1 \leq k ≤109\leq 10^9

  • 0≤0 \leq nums[i] ≤109\leq 10^9

  • edges.length ==== n −1- 1

  • edges[i].length ==2== 2

  • 0≤0 \leq edges[i][0], edges[i][1] ≤\leq n −1- 1

  • The input is generated such that edges represents a valid tree.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find the Maximum Sum of Node Values

Try to solve the Find the Maximum Sum of Node Values problem.

Statement

There exists an undirected tree with n nodes labeled from 00 to n −1- 1. You are given a 00 indexed integer array nums of length n, where nums[i] represents the value of the iextthi^{ ext{th}} node. You are also given a positive integer k and a 2extD2 ext{D} integer array edges of length n −1- 1, where edges[i] == [u, v] indicates an undirected edge between nodes u and v in the tree.

You can perform the following operation any number of times (including zero times):

  • Choose an edge [u, v] connecting nodes u and v.

  • Update nums[u] to be nums[u] XOR k.

  • Update nums[v] to be nums[v] XOR k.

Return the maximum possible sum of the values of all nodes in the tree after applying the operation any number of times.

Constraints:

  • 2≤2 \leq n ==== nums.length ≤2imes104\leq 2 imes 10^4

  • 1≤1 \leq k ≤109\leq 10^9

  • 0≤0 \leq nums[i] ≤109\leq 10^9

  • edges.length ==== n −1- 1

  • edges[i].length ==2== 2

  • 0≤0 \leq edges[i][0], edges[i][1] ≤\leq n −1- 1

  • The input is generated such that edges represents a valid tree.