Chinese Restaurants
InterviewTime limit1sMemory limit128 MB
Pick the intersection that minimizes the farthest distance to a marked restaurant and report that distance, or -1 when no restaurant exists.
Problem
The capital has intersections and roads connecting them. The road network is built so that from any intersection you can reach any other intersection by exactly one route; in other words, the whole network forms a tree. Some intersections have a Chinese restaurant.
Kozik wants to live in the capital and plans to choose an apartment located at one of the intersections. He loves eating but hates walking a lot, so he wants his apartment placed where the distance to the farthest Chinese restaurant is as small as possible, while still being able to visit every Chinese restaurant in the city.
Help Kozik by finding the distance to the farthest Chinese restaurant from the intersection where his chosen apartment sits. Assume the distance between two adjacent intersections is .
Input
The first line contains the number of intersections ().
The second line contains integers (). If is , there is a Chinese restaurant at the -th intersection; if it is , there is none.
Each of the next lines contains two integers and (), meaning intersections and are directly connected by a road.
Output
Print a single integer on one line: the distance to the farthest Chinese restaurant from the apartment Kozik should choose. If the city has no Chinese restaurant, print .