#P3571. [POI 2014] SUP-Supercomputer

[POI 2014] SUP-Supercomputer

Description

Byteasar has designed a supercomputer of novel architecture.

It may comprise of many (identical) processing units.

Each processing unit can execute a single instruction per time unit.

The programs for this computer are not sequential but rather have a tree structure.

Each instruction may have zero, one, or multiple subsequent instructions, for which it is the parent instruction.

The instructions of the program can be executed in parallel on all available processing units. Moreover, they can be executed in many orders: the only restriction is that an instruction cannot be executed unless its parent instruction has been executed before. For example, as many subsequent instructions of an instruction that has been executed already can be executed in parallel as there are processing units.

Byteasar has a certain program to run. Since he likes utilizing his resources optimally, he is wondering how the number of processing units would affect the running time.

He asks you to determine, for a given program and number of processing units, the minimum execution time of the program on a supercomputer with this many processing units.

Equivalent formulation: You are given a rooted tree with nn nodes, with root 11. There are qq queries; in each query, a number kk is given. In one step, you may visit up to kk unvisited vertices whose parents have already been visited. Find the minimum number of steps needed to visit the whole tree for each given kk.

Input Format

In the first line of standard input, there are two integers, nn and qq (1n,q10000001\le n, q\le 1\,000\,000), separated by a single space, that specify the number of instructions in Byteasar's program and the number of running time queries (for different numbers of processing units).

In the second line of input, there is a sequence of qq integers, k1,k2,,kqk_1, k_2, \ldots, k_q (1ki10000001\le k_i\le 1\,000\,000), separated by single spaces: kik_i is the number of processing units in Byteasar's ii-th query.

In the third and last input line, there is a sequence of n1n-1 integers, a2,a3,,ana_2, a_3, \ldots, a_n (1ai<i1\le a_i<i) separated by single spaces: aia_i specifies the number of the parent instruction of instruction number ii. The instructions are numbered with successive integers from 11 to nn, where instruction no. 11 is the first instruction of the program.

Output Format

Your program should print one line consisting of qq integers, separated by single spaces: the ii-th of these numbers should specify the minimum execution time of the program on a supercomputer with kik_i processing units.

20 1
3
1 1 1 3 4 3 2 8 6 9 10 12 12 13 14 11 11 11 11

8

Hint

Translated by ChatGPT 5