cswork / OAOpen practice workspace

AmazonOA #57

Get Max Matching (Match the Column)

Estimated MediumJudge-ready

Topics (AI-labelled)SubsequencesBinary Search

The i-th line connects i to match[i]. Two lines cross only when their index order is the reverse of their target order; lines with the same target do not count as crossing. Find the maximum number of pairwise non-crossing lines that can be kept.

Input

The first line contains n (1..100000), and the second line contains n match values (1..n).

Output

Output a single integer answer.

Example 1

Input:

6
3 1 4 2 3 5

Output:

4

Example 2

Input:

3
1 1 1

Output:

3

Example 3

Input:

3
3 2 1

Output:

1

Example notes

Example 1: keep the four lines with targets 1, 2, 3, 5. Example 2: all targets are equal, which does not meet the strictly-reversed definition of crossing, so all three lines can be kept. Example 3: the targets are strictly decreasing and every two lines cross, so only 1 is kept.

Editorial

Loading solution…