#P1740E. Hanging Hearts
Hanging Hearts
Description
Pak Chanek has blank heart-shaped cards. Card is attached directly to the wall while each of the other cards is hanging onto exactly one other card by a piece of string. Specifically, card () is hanging onto card ().
In the very beginning, Pak Chanek must write one integer number on each card. He does this by choosing any permutation of . Then, the number written on card is .
After that, Pak Chanek must do the following operation times while maintaining a sequence (which is initially empty):
- Choose a card such that no other cards are hanging onto it.
- Append the number written on card to the end of .
- If and the number on card is larger than the number on card , replace the number on card with the number on card .
- Remove card .
After that, Pak Chanek will have a sequence with elements. What is the maximum length of the longest non-decreasing subsequence of at the end if Pak Chanek does all the steps optimally?
A sequence is a subsequence of a sequence if can be obtained from by deletion of several (possibly, zero or all) elements. For example, is a subsequence of , and , but not and .
The first line contains a single integer () — the number of heart-shaped cards.
The second line contains integers () describing which card that each card hangs onto.
Print a single integer — the maximum length of the longest non-decreasing subsequence of at the end if Pak Chanek does all the steps optimally.
Input
The first line contains a single integer () — the number of heart-shaped cards.
The second line contains integers () describing which card that each card hangs onto.
Output
Print a single integer — the maximum length of the longest non-decreasing subsequence of at the end if Pak Chanek does all the steps optimally.
Note
The following is the structure of the cards in the first example.
Pak Chanek can choose the permutation .
Let be the number written on card . Initially, . Pak Chanek can do the following operations in order:
- Select card . Append to the end of . As , the value of becomes . Remove card . After this operation, .
- Select card . Append to the end of . As , the value of is left unchanged. Remove card . After this operation, .
- Select card . Append to the end of . As , the value of is left unchanged. Remove card . After this operation, .
- Select card . Append to the end of . As , the value of becomes . Remove card . After this operation, .
- Select card . Append to the end of . As , the value of is left unchanged. Remove card . After this operation, .
- Select card . Append to the end of . Remove card . After this operation, .
One of the longest non-decreasing subsequences of is . Thus, the length of the longest non-decreasing subsequence of is . It can be proven that this is indeed the maximum possible length.