#P6122. [NEERC 2016] Mole Tunnels

[NEERC 2016] Mole Tunnels

Description

The moles have dug nn burrows underground, connected by n1n-1 tunnels. For any i>1i>1, there is a tunnel between burrow ii and burrow i2\lfloor \frac{i}{2}\rfloor. Burrow ii contains cic_i units of food, which can feed at most cic_i moles. There are mm moles in total, and the ii-th mole lives in burrow pip_i.

One morning, the first kk moles wake up, while the remaining mkm-k moles are asleep. The first kk moles start looking for food. In the end, each of them will arrive at some burrow, such that for every burrow, cic_i is greater than or equal to the number of awake moles in that burrow. The total length of all moles’ movement paths must be minimized. For all 1km1 \le k \le m, output the minimum possible total length of the moles’ movement paths. It is guaranteed that there is always a feasible solution.

Input Format

The first line contains two integers n,mn,m, meaning there are nn burrows and mm moles.

The second line contains nn integers cic_i, representing the amount of food in burrow ii.

The third line contains mm integers pip_i, where pip_i is the burrow where the ii-th mole is located.

Output Format

Output one line with mm integers. The ii-th integer denotes the minimum total length of the moles’ movement paths when k=ik=i.

5 4
0 0 4 1 1
2 4 5 2
1 1 2 4

Hint

For all testdata: 1n,m1051 \le n,m \le 10^5, 0cim0 \le c_i \le m, 1pin1 \le p_i \le n.

Translated by ChatGPT 5