cswork / OAOpen practice workspace

UberOA #47

Maximum Riders in Car

Estimated MediumJudge-ready

Topics (AI-labelled)Prefix & Ranges

Passenger i is only willing to ride with between low[i] and high[i] other passengers. You may choose any subset of passengers to ride together, and every member must be within their own range. Find the maximum number of passengers; the empty set is valid.

This site has made the standard input and output explicit.

Input

The first line contains n, followed by n lines of low high; 1≤n≤100000, 0≤low≤high≤n.

Output

Output one integer, the answer.

Example 1

Input:

4
1 3
2 4
0 2
1 3

Output:

3

Example 2

Input:

3
0 2
0 2
0 2

Output:

3

Example 3

Input:

2
2 2
2 2

Output:

0

Example notes

Example 1: with 3 riders you can pick any three passengers whose ranges cover 2; with 4 riders only 3 ranges cover 3, so the maximum is 3. Example 2: all three accept the other two, so the answer is 3. Example 3: both require 2 others, but there are only 2 people in total, so the answer is 0; this also corrects the original third example, where high=3 exceeded n=2, an invalid range.

Editorial

Loading solution…