City Attractions
Time limit2sMemory limit64 MB
On a weighted tree, Gigel repeatedly jumps from his current city to the city maximizing a_y - dist(x, y), breaking ties by smallest index, and we must report his position after K days.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Binary search, Divide and conquer
- Solved
- No attempts yet
Problem
Moldova has cities, numbered 1 through , and bidirectional roads join some pairs of them. To keep the budget small the government built exactly roads, and you can still travel between any two cities along them.
By national decree every city has a non-negative integer beauty. City has beauty . Two different cities may have equal beauty.
Gigel has arrived in Moldova and is in city 1. He wants to see many beautiful cities, but his time is short and he cannot make up his mind. He stays for more days and visits one city each day. On day he starts from the city he ended the previous day in ( on the first day) and looks for a city with that maximizes , where is the smallest number of roads on a route from to . If several cities reach that maximum, he takes the one with the smallest index. He travels to and spends the rest of day there. The next day he applies the same rule again.
Write a program that finds the city Gigel is in at the end of day .
Input
The first line contains two integers and , the number of cities and the number of days Gigel stays. The second line contains integers , where is the beauty of city . Each of the next lines contains two integers and , meaning that a bidirectional road joins city and city .
Output
Print one integer, the index of the city Gigel is in at the end of day .
Limits
- The roads connect all cities.
Hint
In the first example Gigel leaves city 1 for city 3 on day 1, moves from city 3 to city 5 on day 2, and moves from city 5 to city 2 on day 3. On day 4, the last day, he leaves city 2 for city 5 again.
