#P3714. [BJOI2017] 树的难题
[BJOI2017] 树的难题
Description
You are given an unrooted tree with nodes.
Each edge of the tree has a color. There are colors, numbered from to , and the weight of the -th color is .
For a simple path on the tree, the edges along the path form a color sequence in order, which can be split into several segments of the same color. Define the path value as the sum of the color weights of each same-color segment in the sequence.
Please compute the maximum path value among all simple paths whose number of edges is between and .
Input Format
The first line contains four integers .
The second line contains integers , separated by spaces, representing the weight of each color in order.
The next lines each contain three integers , indicating that there is an edge between node and node with color .
Output Format
Output one line with a single integer, the answer.
5 3 1 4
-1 -5 -2
1 2 1
1 3 1
2 4 2
2 5 3
-1
8 4 3 4
-7 9 6 1
1 2 1
1 3 2
1 4 1
2 5 1
5 6 2
3 7 1
3 8 3
11
Hint
Sample explanation 1: All color weights are negative, and the optimal path is or .
Sample explanation 2: The optimal path is , and its color sequence is .
Constraints | Testpoint ID | | | Special constraints | |-|-|-|-| | | | | None | | | | | None | | | | | The degree of every node is at most | | | | | The degree of every node is at most | | | | | , | | | | | , | | | | | None | | | | | None | | | | | None | | | | | None |
For of the testdata, , , and . It is guaranteed that there exists at least one path whose number of edges is between and .
Translated by ChatGPT 5
京公网安备 11011102002149号