n nodes numbered 0 to n - 1, rooted at node 0. The parent of node i is parent[i] (the root has parent[0] = -1), and its weight is weight[i], which may be negative.
The weight of a subtree is the sum of the weights of all its nodes. Cut off every subtree whose weight is zero, together with all of its nodes.
Return how many nodes remain in the tree.n=6, parent=[-1, 0, 0, 1, 1, 2], weight=[5, 3, 2, -1, -2, 1] → 3
The subtree of node 1 weighs 3 - 1 - 2 = 0, so it goes together with nodes 3 and 4. Nodes 0, 2 and 5 remain (their subtrees weigh 8 and 3).
n=4, parent=[-1, 0, 1, 1], weight=[2, -4, 1, 1] → 0
The whole tree sums to 2 - 4 + 1 + 1 = 0, so all of it is removed.
n=3, parent=[-1, 0, 0], weight=[0, 4, 7] → 3
Node 0 itself weighs zero, but what counts is its subtree sum, 11. Nothing is removed.
parent describes a valid tree rooted at 0n=6, parent=[-1, 0, 0, 1, 1, 2], weight=[5, 3, 2, -1, -2, 1]
3
Run checks the first 3 tests in your browser. Submit checks all 77 tests on the server, hidden ones included.