Problem C
BAAS
BAAS Inc. (Build Airplanes As-a-Service) has grand plans to build lots of airplanes. Naturally, the airplane construction process consists of multiple steps, each of which may take some time. Some steps can take more time to complete than others, e.g. attaching a plane wing may take longer than spray-painting the BAAS logo on the tail. Furthermore, some steps must to be completed before others can commence. For instance the hull needs to be assembled before the wings can be attached to it. If there is no dependency between two steps, then the steps can be completed in parallel.
BAAS is trying very hard to become the market leader in
airplane construction. Since customers value a speedy delivery,
BAAS has decided to invest in their R&D department with the
goal to decrease their delivery time. The chief scientist of
BAAS is very optimistic and thinks that his department can
reduce the time it takes to complete one step to
Input
The first line of the input contains an integer
The next line contains the
This is followed by
It is guaranteed that there is no step that indirectly
depends on itself. Furthermore, each step has a direct or
indirect dependency on step
Output
Output a single integer – the shortest possible time it takes to construct an airplane assuming a single step can be reduced to take no time at all.
Sample Input 1 | Sample Output 1 |
---|---|
2 15 20 0 1 1 |
15 |
Sample Input 2 | Sample Output 2 |
---|---|
4 10 40 70 10 0 1 1 1 1 2 2 3 |
60 |