“It's hard to say that I'd rather stay awake when I'm asleep
'Cause everything is never as it seems (when I fall asleep)”
Adam Young finally woke up. He discovered himself in Number-land. Number-land can be described as a rooted tree of junctions, where junction is the root. Each junction is connected with its parent junction by a road and an integer is written on that road.
Adam will start walking from junction with his board. Initially, his board has written on it. In every second, Adam can walk from his current junction to its parent junction by the road connecting them, or he can finish walking in his current junction. Of course, he cannot walk upward from the root junction. Whenever he crosses a road, he immediately adds the number written on that road to his board’s number.
Adam wants to maximize the final number written on his board after he finished his walking. But he will be sad if it is not divisible by his favorite number . Help Adam finding the maximum number he can get on his board without being sad.
You have to find it for all independently, where is his starting junction.
Help your friend Adam Young, otherwise he will sleep again and stop making best songs.
First line of input will have number of junctions and Adam’s favorite number .
Second line will have integers describing for respectively.
Third line will have integers describing for respectively.
Output a single line consisting of integers. -th integer will be the maximum number Adam can get on his board without being sad, if he starts walking from junction .
Input | Output |
---|---|
7 2 1 1 2 4 3 3 4 -4 -2 2 2 1 | 0 4 0 2 4 2 0 |
This is the tree of sample input and .
For junction , Adam cannot move anywhere.
For junction , and , he will go upto junction .
For junction , it is optimal to move nowhere.
For junction , he will move upto junction .
For junction , it is optimal to move nowhere. Note that, he cannot finish at junction starting from , as his board’s number will not be divisible by .