#P5114. 八月脸

八月脸

Description

Please ignore the nonsense above and pretend you did not see anything.

In one sentence: you are given a tree with nn nodes. Each node is either black or white, and each node has two attributes aa and bb.

Now there are multiple queries. Each query gives only one parameter kk. You need to find a path (u,v)(u,v) in the tree such that uu and vv have different colors, and

$$k\times \sum_{p \in path (u-v)}p.a+\sum_{p\in path(u-v)}p.b$$

is maximized. For each query, you only need to output this maximum value (the two summations mean the sum of attribute aa on the path and the sum of attribute bb on the path, respectively).

Tips: aa, bb, and kk can all be positive or negative, and you are not allowed to choose no path. That is, the maximum value we find may be negative. This happens when the weights of all valid paths are negative.

Input Format

The first line contains two positive integers n,mn,m, representing the number of nodes in the tree and the number of queries.

The next line contains nn integers. The ii-th integer is the value of attribute aa of node ii.

The next line contains nn integers. The ii-th integer is the value of attribute bb of node ii.

The next line contains nn integers, each being either 00 or 11. If the ii-th number is 00, then node ii is a white node; if it is 11, then node ii is a black node.

The next n1n-1 lines each contain two positive integers u,vu,v, indicating that there is an edge between node uu and node vv.

The next mm lines each contain one integer kk, the parameter of the query.

Output Format

Output mm lines. For each query, output the maximum value of the expression given in the statement.

15 15
29 -23 -14 -50 -13 -23 5 33 50 32 27 27 -9 -42 -11
-37 39 21 50 10 -42 -2 25 1 28 40 -45 -24 -29 47
0 0 1 0 0 1 1 0 0 1 0 1 0 0 0
2 1
3 1
4 3
5 2
6 2
7 2
8 4
9 1
10 2
11 5
12 3
13 5
14 3
15 9
-8
36
44
29
-5
-4
-3
-2
-1
0
1
2
3
4
5

679
3252
3988
2608
436
355
274
199
135
126
155
232
309
386
471

Hint

Constraints: 2n1052 \leq n\leq 10^5, 1m1051 \leq m \leq 10^5, 108k108-10^8 \leq k \leq 10^8.

It is guaranteed that there will not be testdata where all nodes are black or all nodes are white. It is guaranteed that for any path in the tree, the absolute value of the sum of attribute aa on the path does not exceed 1.5×1091.5×10^9, and the absolute value of the sum of attribute bb on the path does not exceed 1.5×1091.5×10^9.

Translated by ChatGPT 5