2060: [USACO 2025 January Contest, Gold] Problem 1. Median Heap
文件提交:无需freopen
内存限制:256 MB
时间限制:32.000 S
评测方式:普通裁判
命题人:
提交:3
解决:1
题目描述
Farmer John has a binary tree with
nodes where the nodes are numbered from to ( and is odd). For , the parent of node is . Each node has an initial integer value , and a cost to change the initial value to any other integer value ( ).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
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 (the root) is the median approximation.The FBI has also given Farmer John a list of
independent queries each specified by a target value ( ). 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 .输入
The first line of input contains N.
The next
lines each contain two integers and .The next line contains
.The next
lines each contain a target value .输出
Output Q lines, the minimum possible total cost for each target value m.
样例输入
5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5
样例输出
111
101
101
100
100
100
100
0
11
11
111
提示
To make the median approximation equal
, FJ can change the value at node 3 to . This costs .To make the median approximation equal
, FJ can change the value at node 3 to and the value at node 5 to . This costs .
SCORING:
- Inputs 2-4:
- Inputs 5-7:
- Inputs 8-16: No additional constraints