### Problem Statement

When scheduling tasks to be executed by a person, it is sometimes the case that when one task is completed, a later task suddenly becomes more important. In these situations, it is said that the later task is a 'Supertask' of the completed one.

In addition, when a task reaches a certain level of importance, it must be executed immediately. Each task is initially given a priority of 0. Whenever a task is completed, the priority of its Supertask increases by 1. If at some moment one of the tasks has a priority of 2, and has not been previously executed, it must be executed immediately. If no remaining task has a priority of 2, a single unexecuted task of your choice must be executed (you can not wait and must execute one of them).

### Definition

 Class: PendingTasks Method: latestProcess Parameters: int[] Returns: int Method signature: int latestProcess(int[] supertasks) (be sure your method is public)

### Notes

-If a task is executed after its Supertask, then its execution has no bearing on the priority of the Supertask.
-Tasks can only be executed once

### Constraints

-supertasks will contain between 1 and 50 elements, inclusive
-Element i of supertasks will be between i + 1 and (number of elements in supertasks - 1), inclusive, except the last element of supertasks, which will be -1.

### Examples

0)

 `{4,4,4,4,-1}`
`Returns: 2`
 The final task is a supertask of all other tasks. Whenever you finish any two of first four tasks, the priority of the last will have risen from 0 to 2, so it must execute immediately. This happens after two tasks, so the latest it can start is after two minutes.
1)

 `{1,2,3,4,-1}`
`Returns: 4`
 You can do the tasks in order, putting the final task last, starting it at the fourth minute.
2)

 `{-1}`
`Returns: 0`
 The final task is also the only task, so it must be executed immediately.
3)

 `{6,6,6,7,7,7,8,8,-1}`
`Returns: 7`
 We have the following tree: ``` 8 / \ 6 7 / | \ / | \ 0 1 2 3 4 5 ``` We can not complete all of tasks 0 to 7 before task 8 because: a) one of tasks 0, 1, 2 must be completed after task 6. b) one of tasks 3, 4, 5 must be completed after task 7. c) task 8 must be started as soon as both 6 and 7 are finished - so one of 0 to 5 will not be completed at that moment. One of the possible orderings which starts the final task at the 7th minute is the following: 0, 1, 6, 2, 3, 4, 7, 8.

#### Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6066

#### Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10060&pm=6066

sql_lall

#### Testers:

PabloGilberto , brett1479 , Olexiy

#### Problem categories:

Dynamic Programming, Graph Theory