#P3591. [POI 2015 R3] 访问 Visits
[POI 2015 R3] 访问 Visits
Description
You are given a tree with vertices. Each edge has length , and the weight of vertex is .
Byteasar wants to traverse the whole tree. He will follow a permutation of to and make walks. On the -th walk, he goes from vertex to vertex , and the step size for this walk is .
For a single walk, suppose the start is , the end is , and the step size is . Byteasar starts at and moves forward along the unique path from to , taking steps of exactly edges each time. The testdata guarantees that the distance of each walk is a multiple of .
Please help Byteasar compute, for each walk, the sum of the weights of all vertices he visits during that walk.
Input Format
- The first line contains a positive integer (), the number of vertices.
- The second line contains positive integers, where the -th number is (), the weight of vertex .
- The next lines each contain two positive integers (), indicating that there is an edge between and .
- The next line contains distinct positive integers, where the -th number is (), describing the route (a permutation).
- The next line contains positive integers, where the -th number is (), the step size for the -th walk.
Output Format
Output lines. The -th line should contain one positive integer: the sum of the weights of all vertices visited during the -th walk.
5
1 2 3 4 5
1 2
2 3
3 4
3 5
4 1 5 2 3
1 3 1 1
10
6
10
5
Hint
Original title: Odwiedziny.
Translated by ChatGPT 5
京公网安备 11011102002149号