Tree Points
Given a tree of g_nodes nodes rooted at 0, each node has a value given in array A. There is also a parameter, K.
An unweighted undirected tree is represented with g_nodes nodes numbered from 0 to g_nodes - 1 and g_edges = g_nodes - 1 edges where the ith edge connects the nodes numbered g_from[i] to g_to[i]. Each node numbered u is associated with a value represented by A[u].
Collect all of the values at the nodes using one of two methods:
A[j] - K points.floor(A[j]/2) points. If this method is chosen, all values of node j in this node's subtree are changed to the floor(A[j]/2) as well.You can only collect the value at a node if its parent's value has been collected before (except the root). You have to maximize the total points collected.
Note: It is possible that the accumulated value may be negative at some point.
Complete the function treePoints in the editor.
treePoints has the following parameters:
int g_nodes: the number of nodes in the graphint g_from[g_nodes - 1]: one end of each edgeint g_to[g_nodes - 1]: the other end each edgeint A[g_nodes]: the values at the nodesint K: the cost to collect a full value
Returns
long: the maximum sum of values that can be collected
1Example 1

The first line shows the initial tree. Nodes are labeled as <node number>:<node value>. Darker nodes are ones that change in an operation.
For the first two values, reduce them by K = 5 and collect them. When the value at node 2 is collected as floor(3/2), the value at nodes j below it are changed using the same method, floor(A[j]/2). Hence, the value at node 3 is changed to 1. The value at node 3 is then collected using the second method as floor(1/2). The total collected is 5 + 5 + 1 + 0 = 11.
Constraints
Limits and guarantees your solution can rely on.
- 1 ≤
g_nodes≤ 10^5 g_edges = g_nodes - 1- 0 ≤
g_from[i],g_to[i]<g_nodes g_from[i]≠g_to[i]- 1 ≤
A[i]≤ 10^9 - 1 ≤
K≤ 10^9