**Note: The time limit for this problem is 4s, twice the default.**
Farmer John has a binary tree with N nodes where the nodes are numbered from 1 to N (1≤N<2⋅105 and N is odd). For i>1, the parent of node i is ⌊i/2⌋. Each node has an initial integer value ai, and a cost ci to change the initial value to any other integer value (0≤ai,ci ≤109).
He has been tasked by the Federal Bovine Intermediary (FBI) with finding an approximate median value within this tree, and has devised a clever algorithm to do so.
He starts at the last node N and works his way backward. At every step of the algorithm, if a node would not be the median of it and its two children, he swaps the values of the current node and the child value that would be the median. At the end of this algorithm, the value at node 1 (the root) is the median approximation.
The FBI has also given Farmer John a list of Q (1≤Q≤2⋅105) independent queries each specified by a target value m (0≤m≤109). For each query, FJ will first change some of the node's initial values, and then execute the median approximation algorithm. For each query, determine the minimum possible total cost for FJ to make the output of the algorithm equal to m.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line of input contains N.
The next N lines each contain two integers ai and ci.
The next line contains Q.
The next Q lines each contain a target value m.
OUTPUT FORMAT (print output to the terminal / stdout):
Output Q lines, the minimum possible total cost for each target value m.
SAMPLE INPUT:
5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5
SAMPLE OUTPUT:
111
101
101
100
100
100
100
0
11
11
111
To make the median approximation equal 40, FJ can change the value at node 3 to 60. This costs c3=100.
To make the median approximation equal 45, FJ can change the value at node 3 to 60 and the value at node 5 to 45. This costs c3+c5=100+1=101.
SCORING:
Inputs 2-4: N,Q≤50
Inputs 5-7: N,Q≤1000
Inputs 8-16: No additional constraints
Problem credits: Suhas Nagar and Benjamin Qi
扫码领取USACO试题答案+详细解析
咨询一对一备赛规划