Junoh is top talent!!
Time limit2sMemory limit512 MB
In a weighted tree, find a simple path with the maximum number of nodes, then among those pick the one with the smallest total edge weight, and report that weight divided by T rounded up.
- Level
Medium7 of 10
- Topics
- Tree, DFS, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Junoh got an assignment from his mentor. The problem solving system used in the assignment works like this.
- There are problems and links, and each link joins two different problems. A link works in both directions.
- Starting from any problem, you reach every other problem by following links.
- For any two problems and , exactly one route leads from to .
Junoh picks any one of the problems and starts the test there. The first problem he picks counts as a bonus and is solved in 0 seconds. After solving a problem he chooses one of the links attached to it and moves to the problem on the other end. Passing a link costs the time written on that link, and the time to solve after equals the time to solve after . He can never go back to a problem he has already passed.
Junoh wants to solve as many problems as this system allows. He can spend time on solving problems each day, and when a day's time runs out he continues the rest on the next day. The work may be cut at a day boundary. So if the link times along the chosen route add up to , the number of days is divided by , rounded up. For example, with and a route whose link times are 3, 3, 3, the sum is 9, so the work splits into 4 + 4 + 1 and takes three days.
If several routes pass the maximum number of problems, Junoh takes the one that needs the fewest days. Given , and the links, compute the number of days.
Input
The first line contains the number of problems and the daily solving time (, ). Each of the next lines contains three integers , , (, ). and are the numbers of the two problems joined by a link, and is the time it takes to solve after , or after .
Output
Print the smallest number of days Junoh needs to solve as many problems as possible.