Tree and robots

4.7

3 votes
Tree, Dynamic Programming, Algorithms, Medium
Problem

Given an undirected tree with n vertices. Each edge x has a cost wx. The root of the tree is 1.

You have m robots, each robot starts at root, walks through edges, finally stops at any vertex. When a robot travels through an edge x, you will pay wx.

You need to make each edge to be visited by at least one robot (can be more than one robot).

Your goal is minimize the cost.

Input

The first line contains two integers, n and m.

In the next n1 lines, each line contains three integers, ui,vi,wi, means an edge (ui,vi) exists, and the cost is wi.

It's guaranteed that the given graph is a tree.

Output

An integer representing the minimum cost.

Constraints

1n5104

1m50

1ui,vin

1wi109

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The first robot's route is 123534, and the rest two stay at root.

Editor Image

?