#702
Unrated
Median Heap
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Note: The time limit for this problem is 4s, twice the default.

Farmer John has a binary tree with NN nodes where the nodes are numbered from 11 to NN (1N<21051 \leq N < 2\cdot 10^5 and NN is odd). For i>1i>1, the parent of node ii is i/2\lfloor i/2\rfloor. Each node has an initial integer value aia_i, and a cost cic_i to change the initial value to any other integer value (0ai,ci1090\le a_i,c_i\le 10^9).

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 NN 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 11 (the root) is the median approximation.

The FBI has also given Farmer John a list of QQ (1Q2105)(1 \leq Q \leq 2\cdot 10^5) independent queries each specified by a target value mm (0m1090\le m\le 10^9). 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 mm.

입력

The first line of input contains NN.

The next NN lines each contain two integers aia_i and cic_i.

The next line contains QQ.

The next QQ lines each contain a target value mm.

출력

Output QQ lines, the minimum possible total cost for each target value mm.

예제 입력 1

5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5

예제 출력 1

111
101
101
100
100
100
100
0
11
11
111

점수

Inputs 2-4: N,Q50N,Q\le 50Inputs 5-7: N,Q1000N,Q\le 1000Inputs 8-16: No additional constraints

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.