You are given a tree of size N. The tree isrooted at 1 and each node i (1≤i≤N) contains a value Ai.
You are also given an array K of size N. For each node i (1≤i≤N), you are required to determine the distance to the closest node j in the subtree of i such that ∣Ai−Aj∣≥Ki.
Note
Input format
Output format
Print N lines representing the answer. Here, ith line contains the distance to the closest node j in subtree i that satisfies the condition. If no nodes satisfy the condition, then print −1.
Constraints
1≤N≤200000
1≤Ai≤109
0≤Ki≤109
For node 1, the only valid candidate is node 3 since |A1−A3|≥K1, so the answer is distance between nodes 1 and 3 which is 2.
For node 2, the valid candidates are node 2, and node 3, so the minimum of the distances from 2 is 0.
For node 3, there are no valid candidates, so the answer is -1.