#P6288. [COCI 2016/2017 #1] Kralj

[COCI 2016/2017 #1] Kralj

Description

A war is about to break out between the Dwarven Kingdom and the Elven Kingdom! The elven king Slavko numbers nn elves as 1n1 \dots n. The dwarf king Mirko makes nn dwarves stand in a circle, and starting from some dwarf, numbers them clockwise as 1n1 \dots n.

Next, Mirko assigns each elf a number aia_i, meaning the number of the dwarf that elf will duel. However, due to his carelessness, different elves may be assigned the same number.

To make the duels fair, both sides decide:

  1. Slavko chooses an elf who has not yet been assigned an opponent, and let its number be ii.
  2. If the dwarf numbered aia_i has not yet been assigned an opponent, then that dwarf becomes this elf’s opponent. Otherwise, starting from the dwarf numbered aia_i, choose the first dwarf clockwise along the circle who has not yet been assigned an opponent as this elf’s opponent.
  3. Repeat the above steps until all dwarves and elves have been assigned opponents.

Slavko has collected the strength values of all dwarves and elves. In a duel, the side with the greater strength value always wins. He wants to know: by planning the order in which he chooses elves, what is the maximum number of elves that can win their duels?

Input Format

The first line contains an integer nn.

The second line contains nn integers aia_i.

The third line contains nn integers pip_i, where pip_i is the strength value of dwarf ii.

The fourth line contains nn integers viv_i, where viv_i is the strength value of elf ii.

Output Format

One line with one integer, the maximum number of elves that can win their duels.

3
2 3 3
4 1 10
2 7 3 
2 
4
3 1 3 3
5 8 7 10
4 1 2 6 
1 
3
1 2 3
8 4 3
9 2 6 
2 

Hint

Explanation for Sample 1

Slavko chooses elves in the order 3,2,13, 2, 1.

Then, the opponents of elves 1,2,31, 2, 3 are dwarves 2,1,32, 1, 3, respectively.

Elves 11 and 22 will win their duels.


Constraints

For 100%100\% of the testdata, 1n5×1051 \le n \le 5 \times 10^5, 1ain1 \le a_i \le n, 1pi,vi1091 \le p_i, v_i \le 10^9.


Note

This problem is translated from COCI2016-2017 CONTEST #1 T5 Kralj.

Translated by ChatGPT 5