cswork / OAOpen practice workspace

AmazonOA #288

Min Operations to Sort All Packages

Estimated MediumJudge-ready

Topics (AI-labelled)In-place ArraysHashing

The input is a permutation of 1..n. Initially the next number to collect is 1. In each pass you scan from start to end; whenever you meet the next number, you collect it and increase the target by 1, skipping everything else. Find how many passes it takes to collect 1..n. The real operation is restored from the explicit formula sortingSequence[i]==arrangedCount+1 in the original statement; the introduction is not misread as describing a different target permutation.

Input

The first line contains n and the second line contains sortingSequence. On this site 1≤n≤200000, and the input is guaranteed to be a permutation of 1..n.

Output

Output a single integer, the answer.

Example 1

Input:

4
2 4 1 3

Output:

3

Example 2

Input:

3
1 2 3

Output:

1

Example 3

Input:

3
3 2 1

Output:

3

Example notes

The answers are 3, 1 and 3 respectively. In the first example the three passes collect 1; 2 and 3; and 4. You cannot simply count the descents between adjacent array elements.

Editorial

Loading solution…