Gats and the Ceiling Strings
Gats is exploring a tower of rooms arranged as a rooted tree. Room is the root. For a room on the path from room to room , room is called an ancestor of room ; room is also its own ancestor.
There is a dangling string in every room. The string in room has length .
For each room , Gats has a threshold and a reach limit . A string in an ancestor room is usable from room if both conditions hold:
- .
- The number of edges on the path from room to room is at most .
Among all usable strings from room , Gats chooses one with the smallest number of edges from room . For every room, determine this smallest number. If no usable string exists, the answer for that room is .
Input
The first line contains an integer (), the number of rooms.
The second line contains integers (), where is the length of the string in room .
Output
Print one line containing integers separated by spaces. For each with , must be the smallest number of edges from room to a usable string, or if no usable string exists.
Samples
Sample 1
Input
5 4 2 7 3 6 3 5 6 4 7 0 1 2 1 3 1 1 2 3
Output
0 -1 0 -1 1
In the first sample, rooms and can use their own strings, so their answers are . Room uses the string in room , which is edge above it. Rooms and have no usable string within their reach limits.