QOJ.ac

QOJ

Limite de temps : 12 s Limite de mémoire : 1024 MB Points totaux : 100 Hackable ✓

#2993. Link-Cut Tree

Statistiques

Little L has recently become obsessed with The Legend of Zelda: Breath of the Wild and particularly enjoys the mini-challenges in the game.

There is a challenge in the game called "LCT". The rules are as follows: there is a tree with $N$ nodes, and each edge has an integer weight $v_i$. If $v_i \ge 0$, traversing this edge yields a gain of $v_i$; if $v_i < 0$, traversing this edge requires paying a toll of $-v_i$. Little L needs to control the protagonist Link to cut exactly $K$ edges in the tree, and then add $K$ edges with a weight of $0$ to obtain a new tree. Then, he will choose two nodes $p, q$ in the tree, travel along the simple path connecting these two nodes from $p$ to $q$, and pay the toll / receive the corresponding gain for every edge passed.

TemporaryDO, the god of the Hyrule continent, wants to test Link. He tells Link that if Link can cut the appropriate edges and choose the appropriate path to maximize the total gain minus the total toll, he will give him the legendary Master Sword.

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

Input

The first line contains two positive integers $N$ and $K$, where $0 \le K < N \le 3 \times 10^5$.

The next $N - 1$ lines each contain three integers $x_i, y_i, v_i$, representing that the $i$-th edge connects nodes $x_i$ and $y_i$ with an edge weight of $v_i$.

Output

Output a single integer representing the answer.

Examples

Input 1

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

Output 1

14

Note 1

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

Input 2

See lct/lct2.in in the contestant directory.

Output 2

See lct/lct2.ans in the contestant directory.

Subtasks

  • For 10% of the data, $k = 0$;
  • For another 10% of the data, $k = 1$;
  • For another 15% of the data, $k = 2$;
  • For another 25% of the data, $k \le 100$;
  • For the remaining data, there are no special constraints.

For all test data, it is guaranteed that $1 \le N \le 3 \times 10^5$, $1 \le x_i, y_i \le N$, and $|v_i| \le 10^6$.

Note

The problem is not difficult.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.