Forensic
Time limit2sMemory limit512 MB
Change at most one array entry so the pointer chain starting at index 0 visits as many distinct indices as possible before reaching -1.
Problem

The table above shows the content of an array , with the indices in the top row. The array stores the pointers of a singly linked list, and here a pointer is just an integer value. The pointer of the first node sits in , so the value of is the location of the second node. The pointer of the second node sits in , the pointer of the third node sits in , and so on. A pointer with value is the end of the linked list. In the example above, is 2, so the second pointer sits in . The value of is 4, so the third pointer sits in . The value of is , so no further node follows. The sequence of pointers is written below, and this linked list has 3 nodes.
You received the entries of an array like this, and you were told that one entry was replaced. Neither the position of that entry nor its new value is known. The new value may equal the original one, in which case the array is unchanged. As a forensic expert you want to recover the original array, so you want to modify a single entry and make the modified array represent a linked list with as many nodes as possible. Consider the example above.
- Changing to 6 gives a linked list of 4 nodes.
- Changing to 7 gives a linked list of 4 nodes.
- Changing to 9 gives no valid linked list.
- Changing to 7 gives a linked list of 5 nodes.
Among all changes to one entry, including changing nothing, the recovered list of 5 nodes is the largest. So the original entry of was probably 7. Compute the size of the largest linked list that can be recovered this way.
Input
The first line contains , the size of the array. The second line contains the entries of the array separated by spaces, starting from . Every entry is an integer that is at least and less than .
Output
Print the number of nodes in the largest recovered linked list.