Skip to content

Practice Maximum Score After Applying Operations on a Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2925
LeetCode Problem

Maximum Score After Applying Operations on a Tree

Problem Statement

The tree with edges and values is rooted at 0. Start with score 0. An operation adds a chosen node value to the score and replaces that value with 0. The tree must remain healthy: every root-to-leaf path has a nonzero sum of node values. Return the greatest score obtainable while preserving that condition.

Source: Maximum Score After Applying Operations on a Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1],[0,2],[0,3],[2,4],[4,5]], values = [5,2,5,2,1,1]
Output
11

# Example 2

Input
edges = [[0,1],[0,2],[1,3],[1,4],[2,5],[2,6]], values = [20,10,9,7,4,3,5]
Output
40

Constraints

2 <= n <= 2 * 10^4
edges.length == n - 1
edges[i].length == 2
0 <= ai, bi < n
values.length == n
1 <= values[i] <= 10^9
The input is generated such that edges represents a valid tree.

Related LeetCode Problems

Practice more problems similar to Maximum Score After Applying Operations on a Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.