#P7484. 再生之青
再生之青
Description
YSGH has a tree of size and a sequence of length .
Each node in the tree has a color and a value . All colors are pairwise distinct. It is guaranteed that the number of leaves does not exceed .
Each position in the sequence also has a color and a value . All colors are also pairwise distinct (but colors in the tree and the sequence may be the same).
YSGH wants to choose a path (a chain) in the tree and a subarray (an interval) of the sequence such that each color appears at most once in total. He wants to know the maximum possible sum of values he can obtain.
Input Format
The first line contains two positive integers , representing the size of the tree and the length of the sequence.
The next lines each contain two positive integers , representing an edge in the tree.
The next line contains positive integers; the -th integer is .
The next line contains positive integers; the -th integer is .
The next line contains positive integers; the -th integer is .
The next line contains positive integers; the -th integer is .
Output Format
Only one line containing one integer, representing the answer.
4 4
1 2
2 3
3 4
1 2 3 4
10 2 3 7
2 5 6 1
5 1 7 2
30
Hint
[Sample Explanation]
The optimal solution is to choose nodes in the tree and positions in the sequence.
The sum of values is .
[Constraints]
This problem uses bundled testdata.
For of the testdata, , , , , and it is guaranteed that the number of leaves in the tree does not exceed .
- Subtask 1 (10 points): .
- Subtask 2 (10 points): .
- Subtask 3 (10 points): .
- Subtask 4 (20 points): The -th edge of the tree connects and .
- Subtask 5 (15 points): .
- Subtask 6 (35 points): No additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号