#P12734. 理解

    ID: 11570 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>2025洛谷原创树形 DP洛谷月赛

理解

Description

Saki is practicing modern literature reading conprehension using the method recommended by Yuta.

There are nn historical events, numbered from 11 to nn, where each event may have a prerequisite event with a smaller number, or it may have none. Formally, for event ii, let pip_i denote the number of its prerequisite event, satisfying pi<ip_i < i. If pi=0p_i = 0, it means the event has no prerequisite.

Saki has two ways to recall a historical event: recollection and association. If she recollects, she can spend rur_u time to recall event uu. If she associates, she can choose any event uu that she has already recalled and spend tvt_v time to recall an event vv where pv=up_v = u.

However, her brain capacity is limited, so she can only recall up to kk events simultaneously. She can choose to forget any event she has already recalled at any moment, and forgetting an event does not take any time. To prevent memory confusion, she will not recall any events she has previously forgotten.

Now, she has mm reading questions, and the ii-th question requires her to recall event xix_i. She can solve the ii-th problem immediately upon recalling event xix_i, and the time taken is negligible. She wants to know the minimum amount of time she needs to spend to solve all the questions.

Input Format

The first line contains an integer TT, denoting the number of test cases.

For each test case, the first line contains three integers, n,mn,m and kk, representing the number of historical events, the number of reading questions, respectively and the number of events that she can recall at the same time.

The second line is consisted of nn integers representing p1,,pnp_1,\dots,p_n.

The third line is consisted of nn integers representing r1,,rnr_1,\dots,r_n.

The fourth line is consisted of nn integers representing t1,,tnt_1,\dots,t_n. It is guaranteed that when pi=0p_i=0, ti=0t_i=0.

The fifth line is consisted of mm integers representing x1,,xmx_1,\dots,x_m.

Output Format

For each test case, output a single integer on a line indicating the minimum amount of time she needs to spend to solve all the questions.

2
5 3 3
0 1 1 0 3
1 2 3 4 5
0 1 1 0 2
2 4 5
5 3 2
0 1 1 2 3
1 2 3 4 5
0 1 1 2 2
2 4 5

9
8

Hint

Sample Explanation

For the first test case, the relationships between historical events form the graph as below:

pic

She can perform the following recollection process:

Step Process Time Taken Set of Recalled Events Question Solved
11 Recollects Event 11 11 {1}\{1\}
22 Associates Event 33 {1,3}\{1,3\}
33 Associates Event 55 22 {1,3,5}\{1,3,5\} 33
44 Forgets Event 33 00 {1,5}\{1,5\}
55 Associates Event 22 11 {1,2,5}\{1,2,5\} 11
66 Forgets Event 22 00 {1,5}\{1,5\}
77 Recollects Event 44 44 {1,4,5}\{1,4,5\} 22

Total time taken: 1+1+2+1+4=91+1+2+1+4=9.

Constraints

This problem enables subtask scoring, with the constraints and scores for each subtask as follows.

Subtask No. n,mn,m\le Special Constraint Score Depends on Subtask
11 1010 1818
22 10510^5 A
33 B
44 C
55 2828 1,2,3,41,2,3,4

Special constraint A: pi=0p_i=0 or pi=i1p_i=i-1;

Special constraint B: pi=i2p_i=\lfloor\frac i2\rfloor;

Special constraint C: pi1p_i\le1.

For all of the testdata, 1T51\le T\le5, 1n,m1051\le n,m\le10^5, 1k101\le k\le10, 0pi<i0\le p_i<i, 0ri,ti1090\le r_i,t_i\le10^9, 1xin1\le x_i\le n.