VOOZH about

URL: https://www.geeksforgeeks.org/dsa/collect-maximum-points-from-all-nodes/

⇱ Collect Maximum Points from all Nodes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Collect Maximum Points from all Nodes

Last Updated : 23 Jul, 2025

Given a tree of N nodes and N-1 edges rooted at Node 1, an array points[] of size N, where points[i] denote the number of points at the vertex i and an integer k. A Node can be visited only if its ancestors have already been visited. When any Node i is visited 2 types of operations can be applied:

  • Type 1: Add (points[i] - k) points to total points.
  • Type 2: Add floor(points[i] / 2) points to total points and all the nodes j in the subtree of node i becomes floor(coins[j] / 2).

The task is to determine the maximum points that can be collected from all nodes.

Constraints:

  • 2 <= N <= 10^5
  • 0 <= points [i] <= 10^9
  • 0 <= x <= 10^9

Examples:

Input: N=5, K=4

👁 points_1
Collect Maximum Points from all Nodes

Output: 10
Explanation:

  • Type 1 operation is performed on Node 1, so points obtained = (7 - 4) = 3
  • Type 1 operation is performed on Node 3, so points obtained = (9 - 4) = 5
  • Type 2 operation is performed on Node 4, so points obtained = floor(5/2) = 2 and since Node 4 has no nodes in the subtree, therefore no other node's value gets reduced.
  • Type 2 operation is performed on Node 2, so points obtained = floor(0/2) = 0 and since Node 5 lies in the subtree of node 2, value of node 5 gets reduced to floor(3/2) = 1.
  • Type 2 operation is performed on Node 5, so points obtained = floor(1/2) = 0 and since Node 5 has no nodes in the subtree, therefore no other node's value gets reduced.

Total points obtained = 3 + 5 + 2 + 0 + 0 = 10

Input: N=3, K=0

👁 points_2
Collect Maximum Points from all Nodes

Output: 7
Explanation:

  • Type 1 operation is performed in Node 1, so points obtained = (2 - 0) = 2
  • Type 1 operation is performed in Node 2, so points obtained = (2 - 0) = 2
  • Type 1 operation is performed in Node 3, so points obtained = (3 - 0) = 3

Total points obtained = 2 + 2 + 3 = 7

Approach: The problem can be solved using the following approach:

It can be observed that operation of type 2 can be applied at most 30 times because each type 2 operation reduces the value points at a node i to points[i]/2 and if 30 operations of type 2 are applied then node with any value up to 10^9 will get reduced to 0 as 230>109 so type 2 operation will be applied at most 30 times. Now we can apply Dynamic Programming to solve this problem. The state will be: dp[curr][i] = maximum points that can be collected at the subtree rooted at node curr such that type 2 operation are already applied i times on the ancestors till now. The answer will be max(dp[1][0], dp[1][1], dp[1][2], ...... , dp[1][30]).

Follow the steps to solve the problem:

  • Create a 2D dp[][] array of size N X 30 , where dp[curr][i] represents maximum points that can be collected at the subtree rooted at node curr with type 2 operation already applied i times on the ancestors till now.
  • Iterate through the nodes using DFS. At any node curr, dp[curr][i] will be calculated by:
    • Applying Type 1 Operation: Let sum1 denote the maximum points that can be collected from the subtree rooted at node curr if type 1 operation is performed on node curr. So sum1 will be:
      , where x is child of node curr and pt is the points at node curr when operation 2 have applied i times before visiting node curr.
    • Applying Type 2 Operation: Let sum2 denote the maximum points that can be collected from the subtree rooted at node curr if type 2 operation is performed on node curr. So sum2 will be:
      , where x is child of node curr and pt is the points at node curr when operation 2 have applied i times before visiting node curr.
    • dp[curr][i] will be max(sum1, sum2).
  • The final answer will be max(dp[1][0], dp[1][1], ......, dp[1][29], dp[1][30]).

Below is the implementation of above approach:


Output
10

Time Complexity: O(N), where N is number of nodes in the tree
Auxiliary Space: O(N)

Comment
Article Tags: