|
|||
GiftsFanurie wants to visit each of his N friends and leave a gift of a certain value. He drew a map in the form of an undirected tree with N vertices and found a rule by which he would visit them. From a node X, Fanurie does the following operations:
Fanurie decides to always start from node 1. In order not to upset his friends, he decided to compensate for the delay by increasing the value of the gift. Thus, for each 2 ≤ i ≤ N, v[i] is known – by how much the value of the gift received by the ith visited friend increases compared to the value of the gift received by the i – 1-th visited friend. It is considered that the first friend visited receives the value v[1]. Fanurie defines the subtree induced by a list of nodes as the set of nodes in the minimal subtree that contains each node in the list. It also defines the value of a list of nodes as the sum of the values of the gifts left in the nodes. Fanurie is asking you to help him answer Q queries of the form S K a1 a2 … aK with the following meaning: Which is the node in the subtree induced by the K elements that added to the list of nodes would make the value of the list as close as possible to S? You can not add a node that is already present in the list. If there are several options, choose the smallest node. Fanurie promises to give you a priceless gift if you help him. Input SpecificationThe first line contains two integers N and Q. The second line has N values, the elements of v. Each of the following N – 1 lines have two integers, meaning that there is an edge between them. The following Q lines describe the questions in the form S K a1 a2 … aK. Input Limits and Constraints
Output SpecificationPrint one line to the standard output with Q space-separated integers, representing the answer to each query. Do not put a space after the last number! Sample Input
Output for Sample Input
Explanation
In the first query the induced subtree has the following node-value pairs: {(2,15), (4,21), (5,30), (6,25), (7,28)}. Both nodes 2 and 4 lead to a list value at a distance 3 from S (which is minimum). In the second example node 8 leads to a list value equal to S. |
|||
University of Debrecen; Faculty of Informatics; v. 09/30/2024 |