cswork / OAOpen practice workspace

CitadelOA #11

Optimal Team Size with Skill Bounds

Estimated HardJudge-ready

Topics (AI-labelled)Binary SearchGreedy

Developer i has skill i (1-based). If selected, developer i requires that at most lower[i] team members have lower skill and at most higher[i] have higher skill. Find the largest team in which everyone's requirements are satisfied.

Input

The first line contains n, the second line contains lower, and the third line contains higher; 0≤array values<n. The original OCR025 states n≤200000 and 026 states n≤2000000; this site keeps the wider original domain 1≤n≤2000000 and does not narrow it to the 200,000 of the curated page.

Output

Output a single integer, the answer.

Example 1

Input:

5
1 3 2 2 2
2 2 1 1 3

Output:

3

Example 2

Input:

5
0 4 2 3 3
0 1 3 2 4

Output:

3

Example 3

Input:

2
0 0
0 0

Output:

1

Example notes

The outputs of the three public examples are, in order: 3; 3; 1. These answers were verified by independent enumeration or direct simulation.

Editorial

Loading solution…