#P4383. [八省联考 2018] 林克卡特树

    ID: 3341 远端评测题 10000ms 1000MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>动态规划,dp2018各省省选枚举,暴力树的直径差分

[八省联考 2018] 林克卡特树

Description

Little L has recently become obsessed with The Legend of Zelda: Breath of the Wild, and he especially enjoys the mini challenges.

In the game, there is a challenge called LCT. Its rules are as follows: There is a tree with NN nodes. Each edge has an integer weight viv_i. If vi0v_i \geq 0, traversing this edge yields a gain of viv_i; if vi<0v_i \lt 0, traversing this edge requires paying a toll of vi-v_i. Little L needs to control the protagonist Link to cut exactly KK edges from the tree, then connect KK new edges each with weight 00, producing a new tree. Next, he will choose two nodes p,qp, q on the tree, and walk from pp to qq along the unique simple path connecting them, paying tolls or obtaining gains for each edge he traverses.

The god of Hyrule, TemporaryDO, wants to test Link. He tells Link that if Link can cut appropriate edges and choose an appropriate path so that the total gain - total toll is maximized, he will give him the legendary Master Sword.

Little L wants the Master Sword, so he asks you for help. Please tell him the maximum possible value of total gain - total toll that Link can obtain.

Input Format

The first line contains two positive integers N,KN, K.

The next N1N - 1 lines each contain three integers xi,yi,vix_i, y_i, v_i, indicating that the ii-th edge connects nodes xix_i and yiy_i, and its weight is viv_i.

Output Format

Output a single integer, the answer.

5 1
1 2 3
2 3 5
2 4 -3
4 5 6
14

Hint

Sample Explanation: One possible optimal plan is: cut edge (2,4,3)(2, 4, -3), add edge (3,4,0)(3, 4, 0), and choose (p,q)=(1,5)(p, q) = (1, 5).

Constraints:

  • For 10%10\% of the testdata, K=0K = 0.
  • For another 10%10\% of the testdata, K=1K = 1.
  • For another 15%15\% of the testdata, K=2K = 2.
  • For another 25%25\% of the testdata, K100K \leq 100.
  • For the remaining testdata, there are no special constraints.

For all testdata, it is guaranteed that 1N3×1051 \leq N \leq 3 \times 10^5, 0K3×1050 \leq K \leq 3 \times 10^5, K<NK \lt N, 1xi,yiN1 \leq x_i, y_i \leq N, and vi106|v_i| \leq 10^6.

Hint: This problem is not difficult.

Translated by ChatGPT 5