luogu#P3925. aaa 被​​​​​​​​​​续

    ID: 2881 远端评测题 1500ms 125~250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树树状数组洛谷原创O2优化树链剖分洛谷月赛

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 NN aaa, each with a power value. There are N1N - 1 edges connecting pairs of aaa, so these NN aaa form a rooted tree, rooted at aaa 11.

HansBug wants to "xu" these NN aaa. His strategy is: for the ii-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 nn of them with power values viv_i; then the power value gained from "xu"-ing the ii-th aaa in the queue is v1+v2++viv_1 + v_2 + \cdots + v_i.

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 55 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 100000%10100000 \% 10 points of power value as a reward.

Input Format

The first line contains a positive integer NN, the number of aaa.

The next N1N - 1 lines each contain two positive integers u,vu, v, indicating that there is a subordinate relationship between the uu-th aaa and the vv-th aaa (the highest-level aaa is numbered 11).

The last line contains NN non-negative integers, where the ii-th number is the power value of the ii-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 1000000007\bm{1000000007} (109+7\bm{{10} ^ 9 + 7}).

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 55 aaa is 118+9+10+4+48=189118 + 9 + 10 + 4 + 48 = 189.

After taking modulo 1000000007\bm{1000000007}, the answer is 189\bm{189}.

Constraints

For 30%30\% of the testdata: 1N3×1031 \leq N \leq 3 \times {10}^3.

For 50%50\% of the testdata: 1N2×1041 \leq N \leq 2 \times {10}^4.

For 70%70\% of the testdata: 1N1051 \leq N \leq {10}^5.

For 100%100\% of the testdata: 1N5×1051 \leq N \leq 5 \times {10}^5.

For each aaa's power value aia_i, it is guaranteed that 0ai1080 \leq a_i \leq {10}^8.

Translated by ChatGPT 5