Drawing

Time limit: 2s Memory limit: 1024MB Input: Output:

In 2010, the "Moțoc" kindergarten held an unusual chalk drawing contest in Iași. Each participant had to create a beautiful drawing, but was allowed to lift the chalk off the ground and resume drawing elsewhere only a limited number of times.

Sixteen years later, inspired by that contest, one of the children who took part in it came up with the following problem idea:

A tree is a connected undirected graph without cycles, having NN vertices and exactly N1N - 1 edges. A weighted tree, with NN vertices labeled from 11 to NN, needs to be drawn.

The weight of each edge ee, which is a value w(e)w(e), represents the exact amount of chalk consumed to trace the edge ee.

A drawing of the tree is a way to traverse the tree with a piece of chalk such that every edge is traversed at least once. During the drawing, we are allowed to:

  • retrace edges, meaning that we can traverse them multiple times; each time we traverse an edge, we consume the same amount of chalk;
  • lift the chalk at most KK times, relocating it, after each lift, from its current vertex to any other vertex in the tree (lifting and relocating the chalk is instantaneous and costs nothing).

Note: Placing the chalk on the tree for the very first time at the beginning of the drawing does not count as a lift.

The cost of a drawing is the total amount of chalk used, which is calculated as eEw(e)t(e)\sum_{e \in E} w(e) \cdot t(e), where EE is the set of edges and t(e)1t(e) \geq 1 is the number of times edge ee is traversed during the entire drawing process.

Task

Given a weighted tree with NN vertices and an integer KK, determine the minimum cost of a drawing of the tree that uses at most KK chalk lifts.

Input data

The first line of the input contains two space-separated integers NN and KK, representing the number of vertices and the maximum number of chalk lifts allowed.

The following N1N - 1 lines contain three space-separated integers uu, vv, and ww, meaning there is an undirected edge of weight ww connecting vertices uu and vv.

Output data

The first line of the output must contain a single integer, representing the minimum cost of the drawing.

Constraints and clarifications

  • 0K<N21050 \leq K < N \leq 2 \cdot 10^5
  • 1w(e)1061 \leq w(e) \leq 10^6 for any edge
  • The given graph is guaranteed to be a valid tree.
# Score Constraints
0 0 Examples
1 6 K=0K = 0 and w(e)=1w(e) = 1 for every edge.
2 7 K=0K = 0.
3 3 The degree of every node is at most 22.
4 8 All N1N - 1 edges have one endpoint in vertex 11.
5 11 N200N \leq 200.
6 10 NK107N \cdot K \leq 10^7.
7 12 Every simple path has at most 100100 edges.
8 20 N7104N \leq 7 \cdot 10^4.
9 23 No further restrictions.

Example 1

stdin

7 0
1 2 5
2 3 5
1 4 5
4 5 5
1 6 5
6 7 5

stdout

40

Explanation

With K=0K = 0, the drawing must be a single continuous traversal of the tree.
A valid optimal drawing is: 3214541673 \to 2 \to 1 \to 4 \to 5 \to 4 \to 1 \to 6 \to 7.
There are 2 retraced edges: ([11,44] and [44,55]).

Example 2

stdin

7 1
1 2 5
2 3 5
1 4 5
4 5 5
1 6 5
6 7 5

stdout

30

Explanation

With K=1K = 1, one pen lift is allowed.
An optimal choice is tracing: 321453 \to 2 \to 1 \to 4 \to 5, followed by lifting the chalk and relocating it to vertex 11, then tracing 1671 \to 6 \to 7.

Log in or sign up to be able to send submissions!