Under construction

Still being built. Things may change

Cijepise

Time limit5sMemory limit1024 MB

Summary
For each query node, find the fewest age changes on a rooted tree that let it be invited on the earliest day under the oldest-child swap procedure.
Level

Medium7 of 10

Topics
Tree, Greedy, DFS
Solved
No attempts yet

Statement

Portal cijepise.zdravlje.hr is a technical project that lets residents of the Republic of Croatia sign up for vaccination against COVID-19. The portal cost a little over four million kuna, mainly because a top team of algorithm experts worked on it.

Little Ante loves programming, coconut ice cream, and following epidemic measures. Of course, he immediately signed his QQ close friends up for vaccination through the portal. He remembers the exact date: it was March 1st, and he was preparing for the county informatics competition. In the meantime the national competition took place, the Croatian Logo Olympiad was held, and Chelsea reached the Champions League final. However, none of Ante's friends received a vaccination invitation.

Ante decided to take matters into his own hands. He sent messages, called the help line, intercepted network traffic, and compiled and decompiled code. After a few hours he worked out how the portal runs and gained access to the data of all users. Now he needs help from real algorithm experts.

Users are stored internally in a tree. Each of the NN users is one of NN nodes of a simple connected graph with (N−1)(N - 1) edges. Nodes are labeled with natural numbers from 11 to NN, and node 11 is the root. The invitation algorithm starts by sending an invitation to the user at the root. That user is removed from the system, and the root becomes empty. Then a complicated node-moving procedure runs for exactly 24 hours, after which the next user to be invited appears at the root.

The node-moving procedure first swaps the root with its child of the greatest age. Now the root holds some user, and one of its children is empty. Then the procedure swaps the empty child with its oldest child, and so on, until one of the leaves becomes empty. That leaf is then removed from the tree. If at any step a node has several children of the same greatest age, the algorithm picks one of the oldest children at random.

An example of the node-moving procedure (values are user ages).

For each of the QQ friends, Ante wants the minimum number of users whose age he must change so that that friend is invited with certainty in the smallest possible number of days. Ante can change a user's age to any non-negative integer less than or equal to 2⋅1092 \cdot 10^9.

Input

The first line contains the natural number NN from the statement.

The second line contains NN integers xix_i (1≤xi≤1091 \le x_i \le 10^9), where xix_i is the age of the user at node ii.

Each of the next N−1N - 1 lines contains natural numbers aia_i and bib_i (1≤ai,bi≤N1 \le a_i, b_i \le N), meaning there is an edge between nodes aia_i and bib_i.

The next line contains the natural number QQ from the statement.

Each of the next QQ lines contains a natural number qiq_i (1≤qi≤N1 \le q_i \le N), the node where the ii-th friend is located.

Output

For each of the QQ friends, print on its own line the minimum number of users whose age Ante must change so that the ii-th friend is invited in the smallest possible number of days.

Examples3

  1. Example 1

    Input
    3
    1 2 3
    1 2
    1 3
    3
    1
    2
    3
    
    Expected output
    0
    1
    0
    
  2. Example 2

    Input
    7
    45 13 19 3 81 27 77
    1 2
    1 3
    1 4
    3 5
    3 6
    4 7
    3
    5
    6
    7
    
    Expected output
    0
    1
    1
    
  3. Example 3

    Input
    8
    23 4 9 7 11 4 1 18
    2 1
    3 2
    4 2
    5 2
    6 3
    7 4
    8 1
    3
    2
    3
    7
    
    Expected output
    1
    2
    3