Skip to content

Practice Maximum Points After Collecting Coins From All Nodes

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

Hard#2920
LeetCode Problem

Maximum Points After Collecting Coins From All Nodes

Problem Statement

The tree is rooted at node 0, with undirected edges and coins[i] initially at node i. Collect a node only after all its ancestors. At each node, either collect its current coin count and gain that count minus k points, or gain floor(current count / 2) points and halve the coin counts of every node in its subtree, rounding down. Halving effects accumulate. Return the greatest total after collecting every node.

Source: Maximum Points After Collecting Coins From All Nodes on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
Output
11

# Example 2

Input
edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
Output
16

Constraints

n == coins.length
2 <= n <= 10^5
0 <= coins[i] <= 10^4
edges.length == n - 1
0 <= edges[i][0], edges[i][1] < n
0 <= k <= 10^4

Related LeetCode Problems

Practice more problems similar to Maximum Points After Collecting Coins From All Nodes.

Review your practice with PhantomCodeAI

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