#P1425I. Impressive Harvesting of The Orchard
Impressive Harvesting of The Orchard
No submission language available for this problem.
Description
Mr. Chanek has an orchard structured as a rooted ternary tree with vertices numbered from to . The root of the tree is vertex . denotes the parent of vertex , for . Interestingly, the height of the tree is not greater than . Height of a tree is defined to be the largest distance from the root to a vertex in the tree.
There exist a bush on each vertex of the tree. Initially, all bushes have fruits. Fruits will not grow on bushes that currently already have fruits. The bush at vertex will grow fruits after days since its last harvest.
Mr. Chanek will visit his orchard for days. In day , he will harvest all bushes that have fruits on the subtree of vertex . For each day, determine the sum of distances from every harvested bush to , and the number of harvested bush that day. Harvesting a bush means collecting all fruits on the bush.
For example, if Mr. Chanek harvests all fruits on subtree of vertex , and harvested bushes , the sum of distances is
in a tree is defined to be the number of edges on the simple path from to .
The first line contains two integers and , which denotes the number of vertices and the number of days Mr. Chanek visits the orchard.
The second line contains integers , which denotes the fruits growth speed on the bush at vertex , for .
The third line contains integers , which denotes the parent of vertex in the tree, for . It is guaranteed that each vertex can be the parent of at most other vertices. It is also guaranteed that the height of the tree is not greater than .
The next lines contain a single integer , which denotes the start of Mr. Chanek's visit on day , for .
Output lines, line gives the sum of distances from the harvested bushes to , and the number of harvested bushes.
Input
The first line contains two integers and , which denotes the number of vertices and the number of days Mr. Chanek visits the orchard.
The second line contains integers , which denotes the fruits growth speed on the bush at vertex , for .
The third line contains integers , which denotes the parent of vertex in the tree, for . It is guaranteed that each vertex can be the parent of at most other vertices. It is also guaranteed that the height of the tree is not greater than .
The next lines contain a single integer , which denotes the start of Mr. Chanek's visit on day , for .
Output
Output lines, line gives the sum of distances from the harvested bushes to , and the number of harvested bushes.
Samples
Note
For the first example:
- On day 1, Mr. Chanek starts at vertex and can harvest the bush at vertex 2.
- On day 2, Mr. Chanek starts at vertex and only harvest from bush (bush 2's fruit still has not grown yet).
- On day 3, Mr. Chanek starts at vertex and harvests the fruits on bush and . The sum of distances from every harvested bush to is .
For the second example, Mr. Chanek always starts at vertex . The bushes which Mr. Chanek harvests on day one, two, and three are , respectively.