Crystalfly
Time limit2sMemory limit1024 MB
Given a tree where each vertex has flies and a 1 to 3 second disappearance timer, find the most flies Paimon can catch starting from vertex 1.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Tree, DFS
- Solved
- No attempts yet
Problem
Paimon is catching crystalflies on a tree, which are a special kind of butterflies in Teyvat. A tree is a connected graph consisting of vertices and undirected edges.

Pixiv ID: 93964680
There are initially crystalflies on the -th vertex. When Paimon reaches a vertex, she can catch all the remaining crystalflies on the vertex immediately. However, the crystalflies are timid. When Paimon reaches a vertex, all the crystalflies on the adjacent vertices will be disturbed. For the -th vertex, if the crystalflies on the vertex are disturbed for the first time at the beginning of the -th second, they will disappear at the end of the -th second.
At the beginning of the -th second, Paimon reaches vertex and stays there before the beginning of the -st second. Then at the beginning of each following second, she can choose one of the two operations:
- Move to one of the adjacent vertices of her current vertex and stay there before the beginning of the next second (if the crystalflies in the destination will disappear at the end of that second, she can still catch them).
- Stay still in her current vertex before the beginning of the next second.
Calculate the maximum number of crystalflies Paimon can catch in seconds.
Input
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains an integer () indicating the number of vertices.
The second line contains integers (), where is the number of crystalflies on the -th vertex.
The third line contains integers (), where is the time before the crystalflies on the -th vertex disappear after being disturbed.
For the next lines, the -th line contains two integers and () indicating an edge connecting vertices and in the tree.
The sum of over all test cases does not exceed .
Output
For each test case, output one line containing one integer: the maximum number of crystalflies Paimon can catch.
Hint
For the first sample test case, follow the strategy below.
-
During the -th second
- Paimon arrives at vertex ;
- Paimon catches crystalfly;
- Crystalflies in vertices and are disturbed.
-
During the -st second
- Paimon arrives at vertex ;
- Paimon catches crystalflies.
-
During the -nd second
- Paimon arrives at vertex ;
- Crystalflies in vertex disappear.
-
During the -rd second
- Paimon arrives at vertex ;
- Crystalflies in vertices and are disturbed.
-
During the -th second
- Paimon arrives at vertex ;
- Paimon catches crystalflies;
- Crystalflies in vertex disappear.
For the second sample test case, the optimal strategy is the same as for the first. The crystalflies in vertex are scheduled to disappear at the end of the -rd second (instead of the -nd), which lets Paimon catch them.