Maximum Points After Collecting Coins From All Nodes Problem
Description
LeetCode Problem 2920.
There exists an undirected tree rooted at node 0 with n nodes labeled from 0 to n - 1. You are given a 2D integer array edges of length n - 1, where edges[i] = [a_i, b_i] indicates that there is an edge between nodes a_i and b_i in the tree. You are also given a 0-indexed array coins of size n where coins[i] indicates the number of coins in the vertex i, and an integer k.
Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.
Coins at node_i can be collected in one of the following ways:
- Collect all the coins, but you will get
coins[i] - kpoints. Ifcoins[i] - kis negative then you will loseabs(coins[i] - k)points. - Collect all the coins, but you will get
floor(coins[i] / 2)points. If this way is used, then for all thenode_jpresent in the subtree ofnode_i,coins[j]will get reduced tofloor(coins[j] / 2).
Return the maximum points you can get after collecting the coins from all the tree nodes.
Example 1:

Input: edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
Output: 11
Explanation:
Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
Example 2:

Input: edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
Output: 16
Explanation:
Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
Constraints:
n == coins.length2 <= n <= 10^50 <= coins[i] <= 10^4edges.length == n - 10 <= edges[i][0], edges[i][1] < n0 <= k <= 10^4
Solution
Approach: Memoized DFS
Build the adjacency lists of the tree. Let dfs(i, fa, j) be the best score of the subtree of node i (parent fa) when the coins of every node in it have already been halved j times, i.e. each node holds coins[i] >> j.
At node i there are two choices:
- Take
coins[i] >> jcoins and payk: the score is(coins[i] >> j) - kplusdfs(c, i, j)over the childrenc. - Take the points
coins[i] >> (j + 1), which halves everything below: the score is that value plusdfs(c, i, j + 1)over the children.
Return the larger one. Since coins[i] <= 10^4 < 2^14, after 14 halvings everything is 0, so j stays below 15 and we cache dfs in a table f[i][j]. The answer is dfs(0, -1, 0).
Complexity: O(n log M) time and space, where M is the largest coin value.
from functools import lru_cache
class Solution:
def maximumPoints(self, edges: List[List[int]], coins: List[int], k: int) -> int:
@lru_cache(None)
def dfs(i: int, fa: int, j: int) -> int:
a = (coins[i] >> j) - k
b = coins[i] >> (j + 1)
for c in g[i]:
if c != fa:
a += dfs(c, i, j)
if j < 14:
b += dfs(c, i, j + 1)
return max(a, b)
n = len(coins)
g = [[] for _ in range(n)]
for a, b in edges:
g[a].append(b)
g[b].append(a)
ans = dfs(0, -1, 0)
dfs.cache_clear()
return ans