#P7484. 再生之青

再生之青

Description

YSGH has a tree of size nn and a sequence of length mm.

Each node ii in the tree has a color cic_i and a value aia_i. All colors are pairwise distinct. It is guaranteed that the number of leaves does not exceed 20\boldsymbol{20}.

Each position ii in the sequence also has a color did_i and a value bib_i. 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 n,mn, m, representing the size of the tree and the length of the sequence.

The next n1n - 1 lines each contain two positive integers x,yx, y, representing an edge in the tree.

The next line contains nn positive integers; the ii-th integer is cic_i.

The next line contains nn positive integers; the ii-th integer is aia_i.

The next line contains mm positive integers; the ii-th integer is did_i.

The next line contains mm positive integers; the ii-th integer is bib_i.

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 1,2,3,41, 2, 3, 4 in the tree and positions 2,32, 3 in the sequence.

The sum of values is 10+2+3+7+1+7=3010 + 2 + 3 + 7 + 1 + 7 = 30.


[Constraints]

This problem uses bundled testdata.

For 100%100 \% of the testdata, 1n5×1041 \le n \le 5 \times {10}^4, 1m5×1051 \le m \le 5 \times {10}^5, 1ci,din+m1 \le c_i, d_i \le n + m, 1ai,bi1091 \le a_i, b_i \le {10}^9, and it is guaranteed that the number of leaves in the tree does not exceed 2020.

  • Subtask 1 (10 points): n,m500n, m \le 500.
  • Subtask 2 (10 points): n,m1000n, m \le 1000.
  • Subtask 3 (10 points): n,m5000n, m \le 5000.
  • Subtask 4 (20 points): The ii-th edge of the tree connects ii and i+1i + 1.
  • Subtask 5 (15 points): m105m \le {10}^5.
  • Subtask 6 (35 points): No additional constraints.

Translated by ChatGPT 5