luogu#P3925. aaa 被续
aaa 被续
Background
HansBug keeps being bored...
Problem Description
Because aaa did not finish HansBug's task, HansBug starts planning how to "xu" aaa.
Now HansBug has aaa, each with a power value. There are edges connecting pairs of aaa, so these aaa form a rooted tree, rooted at aaa .
HansBug wants to "xu" these aaa. His strategy is: for the -th aaa, first let him and all of his descendant aaa stand obediently ♂ in a queue, then "xu" them one by one.
After long research on aaa power values, HansBug found that, for each such queue of aaa, suppose there are of them with power values ; then the power value gained from "xu"-ing the -th aaa in the queue is .
However, the relationship tree among aaa is quite complex, and HansBug has run out of IQ, so this task is handed over to you. But HansBug does know that no aaa has more than direct child aaa.
HansBug wants to know, with the optimal queuing method each time, the maximum total power value obtainable after "xu"-ing all these aaa, and then give you points of power value as a reward.
Input Format
The first line contains a positive integer , the number of aaa.
The next lines each contain two positive integers , indicating that there is a subordinate relationship between the -th aaa and the -th aaa (the highest-level aaa is numbered ).
The last line contains non-negative integers, where the -th number is the power value of the -th aaa.
Output Format
Output a single integer, the maximum total power value HansBug can obtain after "xu"-ing all aaa.
Since the result can be large, take it modulo ().
5
5 3
1 2
1 5
4 5
3 9 10 4 7
189
Hint
Sample Explanation

Therefore, the maximum total power value from "xu"-ing aaa is .
After taking modulo , the answer is .
Constraints
For of the testdata: .
For of the testdata: .
For of the testdata: .
For of the testdata: .
For each aaa's power value , it is guaranteed that .
Translated by ChatGPT 5