#P3714. [BJOI2017] 树的难题

    ID: 1362 远端评测题 2000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017各省省选点分治单调队列分治深度优先搜索,DFS

[BJOI2017] 树的难题

Description

You are given an unrooted tree with nn nodes.

Each edge of the tree has a color. There are mm colors, numbered from 11 to mm, and the weight of the ii-th color is cic_i.

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 ll and rr.

Input Format

The first line contains four integers n,m,l,rn, m, l, r.

The second line contains mm integers c1,c2,,cmc_1, c_2, \ldots, c_m, separated by spaces, representing the weight of each color in order.

The next n1n-1 lines each contain three integers u,v,cu, v, c, indicating that there is an edge between node uu and node vv with color cc.

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 (1,2)(1, 2) or (1,3)(1, 3).

Sample explanation 2: The optimal path is (3,1,2,5,6)(3, 1, 2, 5, 6), and its color sequence is (2,1,1,2)(2, 1, 1, 2).

Constraints | Testpoint ID | nn | mm | Special constraints | |-|-|-|-| | 11 | =103=10^3 | n\le n | None | | 22 | =104=10^4 | =2=2 | None | | 33 | =105=10^5 | n\le n | The degree of every node is at most 22 | | 44 | =2×105=2\times10^5 | n\le n | The degree of every node is at most 22 | | 55 | =105=10^5 | =10=10 | l=1l=1, r=n1r=n-1 | | 66 | =2×105=2\times10^5 | n\le n | l=1l=1, r=n1r=n-1 | | 77 | =105=10^5 | =50=50 | None | | 88 | =105=10^5 | n\le n | None | | 99 | =2×105=2\times 10^5 | =100=100 | None | | 1010 | =2×105=2\times 10^5 | n\le n | None |

For 100%100\% of the testdata, 1n,m2×1051 \leq n, m \leq 2 \times 10^5, 1lrn1 \leq l \leq r \leq n, and ci104\lvert c_i \rvert \leq 10^4. It is guaranteed that there exists at least one path whose number of edges is between ll and rr.

Translated by ChatGPT 5