CF BUDDY
← Problems·

1139E · Maximize Mex

2400 · flows, graph matchings, graphs

Problem: There are nn students and mm clubs in a college. The clubs are numbered from 11 to mm. Each student has a potential pip_i and is a member of the club with index cic_i. Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next dd days. There is a coding competition every day in the technical fest.

Every day, in the morning, exactly one student of the college leaves their club. Once a student leaves their club, they will never join any club again. Every day, in the afternoon, the director of the college will select one student from each club (in case some club has no members, nobody is selected from that club) to form a team for this day's coding competition. The strength of a team is the mex of potentials of the students in the team. The director wants to know the maximum possible strength of the team for each of the coming dd days. Thus, every day the director chooses such team, that the team strength is maximized.

The mex of the multiset SS is the smallest non-negative integer that is not present in SS. For example, the mex of the {0,1,1,2,4,5,9}\{0, 1, 1, 2, 4, 5, 9\} is 33, the mex of {1,2,3}\{1, 2, 3\} is 00 and the mex of \varnothing (empty set) is 00.

Input Format: The first line contains two integers nn and mm (1mn50001 \leq m \leq n \leq 5000), the number of students and the number of clubs in college.

The second line contains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n (0pi<50000 \leq p_i < 5000), where pip_i is the potential of the ii-th student.

The third line contains nn integers c1,c2,,cnc_1, c_2, \ldots, c_n (1cim1 \leq c_i \leq m), which means that ii-th student is initially a member of the club with index cic_i.

The fourth line contains an integer dd (1dn1 \leq d \leq n), number of days for which the director wants to know the maximum possible strength of the team.

Each of the next dd lines contains an integer kik_i (1kin1 \leq k_i \leq n), which means that kik_i-th student lefts their club on the ii-th day. It is guaranteed, that the kik_i-th student has not left their club earlier.

Output Format: For each of the dd days, print the maximum possible strength of the team on that day.

Note: Consider the first example:

On the first day, student 33 leaves their club. Now, the remaining students are 11, 22, 44 and 55. We can select students 11, 22 and 44 to get maximum possible strength, which is 33. Note, that we can't select students 11, 22 and 55, as students 22 and 55 belong to the same club. Also, we can't select students 11, 33 and 44, since student 33 has left their club.

On the second day, student 22 leaves their club. Now, the remaining students are 11, 44 and 55. We can select students 11, 44 and 55 to get maximum possible strength, which is 11.

On the third day, the remaining students are 11 and 55. We can select students 11 and 55 to get maximum possible strength, which is 11.

On the fourth day, the remaining student is 11. We can select student 11 to get maximum possible strength, which is 11.

On the fifth day, no club has students and so the maximum possible strength is 00.

Sample Cases

Case 1

Input

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

Output

3
1
1
1
0

Case 2

Input

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

Output

3
2
2
1
0

Case 3

Input

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

Output

1
1
1
1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback