There is an undirected tree with n nodes labeled from 0 to n - 1, and rooted at node 0. You are given a 2D integer array edges of length n -1, where edges[i] = [ai, bi] indicates that there is an edge between nodes
ai and bi in the tree.
You are also given a 0-indexed integer array values of length n, where
values[i] is the value associated with the ith node.
You start with a score of 0. In one operation, you can:
Pick any node i.
Add values[i] to your score.
Set values[i] to 0.
A tree is healthy if the sum of values on the path from the root to any leaf node is different than zero.
Return themaximum score you can obtain after performing these operations on the tree any number of times so that it remains healthy.

Input: edges =[[0,1],[0,2],[0,3],[2,4],[4,5]], values =[5,2,5,2,1,1]Output: 11Explanation: We can choose nodes 1,2,3,4, and 5. The value of the root is non-zero. Hence, the sum of values on the path from the root to any leaf is different than zero. Therefore, the tree is healthy and the score is values[1]+ values[2]+ values[3]+ values[4]+ values[5]=11.It can be shown that 11is the maximum score obtainable after any number of operations on the tree.

Input: edges =[[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values =[20,10,9,7,4,3,5]Output: 40Explanation: We can choose nodes 0,2,3, and 4.- The sum of values on the path from 0 to 4is equal to 10.- The sum of values on the path from 0 to 3is equal to 10.- The sum of values on the path from 0 to 5is equal to 3.- The sum of values on the path from 0 to 6is equal to 5.Therefore, the tree is healthy and the score is values[0]+ values[2]+ values[3]+ values[4]=40.It can be shown that 40is the maximum score obtainable after any number of operations on the tree.
We want to maximize the sum of selected node values, but after any number of operations, the tree must remain “healthy”: the sum of values on the path from the root to any leaf must not be zero. If we set a node’s value to zero, all its descendants’ path sums are affected. The only way the tree becomes unhealthy is if all values on a root-to-leaf path are set to zero. So, the minimum sum among all root-to-leaf paths must remain nonzero.
classSolution {
public:int maximumScoreAfterOperations(vector<vector<int>>& edges, vector<int>& values) {
int n = values.size();
vector<vector<int>> g(n);
for (auto& e : edges) {
g[e[0]].push_back(e[1]);
g[e[1]].push_back(e[0]);
}
function<longlong(int, int)> minPath = [&](int u, int p) {
if (g[u].size() ==1&& u !=0) return (longlong)values[u];
longlong res = LLONG_MAX;
for (int v : g[u]) {
if (v == p) continue;
res = min(res, minPath(v, u));
}
return res + values[u];
};
longlong total = accumulate(values.begin(), values.end(), 0LL);
longlong minSum = LLONG_MAX;
for (int v : g[0]) minSum = min(minSum, minPath(v, 0));
return total - minSum;
}
};
classSolution {
publicintmaximumScoreAfterOperations(int[][] edges, int[] values) {
int n = values.length;
List<Integer>[] g =new ArrayList[n];
for (int i = 0; i < n; i++) g[i]=new ArrayList<>();
for (int[] e : edges) {
g[e[0]].add(e[1]);
g[e[1]].add(e[0]);
}
int total = 0;
for (int v : values) total += v;
int minSum = Integer.MAX_VALUE;
for (int v : g[0]) minSum = Math.min(minSum, minPath(g, values, v, 0));
return total - minSum;
}
privateintminPath(List<Integer>[] g, int[] values, int u, int p) {
if (g[u].size() == 1 && u != 0) return values[u];
int res = Integer.MAX_VALUE;
for (int v : g[u]) {
if (v == p) continue;
res = Math.min(res, minPath(g, values, v, u));
}
return res + values[u];
}
}
classSolution {
funmaximumScoreAfterOperations(edges: Array<IntArray>, values: IntArray): Int {
val n = values.size
val g = Array(n) { mutableListOf<Int>() }
for (e in edges) {
g[e[0]].add(e[1])
g[e[1]].add(e[0])
}
funminPath(u: Int, p: Int): Int {
if (g[u].size ==1&& u !=0) return values[u]
var res = Int.MAX_VALUE
for (v in g[u]) {
if (v == p) continue res = minOf(res, minPath(v, u))
}
return res + values[u]
}
val total = values.sum()
var minSum = Int.MAX_VALUE
for (v in g[0]) minSum = minOf(minSum, minPath(v, 0))
return total - minSum
}
}
classSolution:
defmaximumScoreAfterOperations(self, edges: list[list[int]], values: list[int]) -> int:
n = len(values)
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
defmin_path(u, p):
if len(g[u]) ==1and u !=0:
return values[u]
res = float('inf')
for v in g[u]:
if v == p: continue res = min(res, min_path(v, u))
return res + values[u]
total = sum(values)
min_sum = float('inf')
for v in g[0]:
min_sum = min(min_sum, min_path(v, 0))
return total - min_sum