#P5351. Ruri Loves Maschera

Ruri Loves Maschera

Description

The world in Ruri’s novel has nn cities and n1n-1 roads, with no multiple edges or self-loops. Among these n1n-1 roads, the magic value of the ii-th road is wiw_i.

As the Queen of the Night Demons, Ruri decides to tour the entire dark world. Each time, she randomly chooses a city as the starting point, and ends at some city after passing through at least LL roads and at most RR roads. She never goes back along the same road during the tour. If the magic values of the roads she passes through in one tour are v1,v2,...,vkv_1, v_2, ..., v_k (LkR)(L \leq k \leq R) in order, then she gains a magic value of max(v1,v2,...,vk)\max(v_1, v_2, ..., v_k).

Now Ruri wants to know: after trying all valid tour routes, what is the total sum of the magic values she gains?

Note that the path from xx to yy and the path from yy to xx are considered two different paths.

Input Format

The first line contains three integers n,L,Rn, L, R.

The next n1n-1 lines each contain three integers x,y,wx, y, w, indicating that there is a road between node xx and node yy with magic value ww.

Output Format

Output the total sum of the magic values she gains.

5 2 3
1 2 2
2 3 2
3 4 4
4 5 5
40

Hint

Constraints:

For 10%10\% of the testdata, n5000n \leq 5000.

For another 10%10\% of the testdata, min(x,y)=1\min(x, y) = 1.

For another 15%15\% of the testdata, xy=1|x - y| = 1.

For another 25%25\% of the testdata, L=1,R=n1L = 1, R = n - 1.

For 100%100\% of the testdata, n105n \leq 10^5, 1LRn11 \leq L \leq R \leq n - 1, and 1wi1051 \leq w_i \leq 10^5.

Translated by ChatGPT 5