Loading...
You are given a rooted tree with n nodes labelled 1 to n, described as n - 1 directed edges edges[i] = [parent, child]; exactly one node has no parent and is the root. Every node starts with a value of 0.
You must also perform tasks.length tasks, given by tasks, where each task tasks[i] = [delta, c1, c2, ..., ck] (k >= 1) requires you to choose exactly one of the listed candidate nodes and add delta (which may be negative) to that node and to every node in its subtree.
Choose the candidates so that the sum of all node values after every task is as large as possible. Return that sum.
Input: n = 3, edges = [[1,2],[1,3]], tasks = [[10,2,3]]
Output: 10
Explanation: Nodes 2 and 3 are leaves; either choice adds 10 to a single node.
Input: n = 4, edges = [[1,2],[2,3],[2,4]], tasks = [[5,1,2],[-3,2,3]]
Output: 17
Explanation: Task 1: choosing node 1 adds 5 to all four nodes (20), which beats node 2's three nodes. Task 2: choosing the leaf 3 subtracts 3 from one node (-3), which beats node 2's three nodes (-9). Total 20 - 3 = 17.
Input: n = 2, edges = [[2,1]], tasks = [[-4,2]]
Output: -8
Explanation: The only candidate is the root; both nodes lose 4.
edges.length == n - 1; the edges form a single rooted treetasks.length ≤105; the total number of candidates over all tasks is at most 2⋅105delta ≤104; every candidate is a valid node labelClick "Run" to test with sample cases or "Submit" to run all tests.