This page is still under construction.

Parts of this page are still being built. What you see may change.

Agamemnon's Odyssey

Time limit1sMemory limit1024 MB

Summary
Given a weighted tree and a limit k, find a walk (any start and end) that uses no edge more than k times and collects the weight of each edge only on its first traversal, maximizing total collected weight.
Level

Hard8 of 10

Topics
Tree, Greedy, Dynamic programming, DFS
Solved
No attempts yet

Problem

Example map

Agamemnon, the great king of Mycenae, was assembling his troops in Aulis to sail to the shores of Troy, when he had a vision of goddess Artemis. In this vision, Agamemnon found out that he had accidentally slain a deer that was sacred to Artemis, and now the goddess swore to make Agamemnon suffer on his voyage to Troy.

Along his route to Troy, Agamemnon was planning to stop at the islands of Crete to gather resources for his formidable army. If Artemis were to find out about the sea routes Agamemnon took, she would use her powers to stop the wind along those routes, leaving Agamemnon and his crew stranded. As the trusty advisor of Agamemnon, you now have to help him devise a path between the islands of Crete that provides the army the maximum amount of resources, without letting Artemis discover the routes you take.

The NN islands of Crete are connected to each other via N−1N-1 sea routes. Along each route, Agamemnon can acquire a certain amount of resources. However, if a route is used more than kk times, Artemis will detect the presence of Agamemnon along that route and stop the wind along that route. So, a feasible plan cannot use any route more than kk times.

Given that Agamemnon can start and end at any of the islands of Crete, come up with a feasible plan that maximises Agamemnon's resource earnings. Note that Agamemnon can only collect resources along a sea route during its first use. He does not earn extra resources from a route on reusing it.

Input

The first line of input will contain two integers, NN (1≤N≤2⋅1051 \leq N \leq 2 \cdot 10^5) and kk (1≤k≤1091 \leq k \leq 10^9), the number of islands of Crete, and the maximum number of times a single route may be used without being discovered by Artemis. The islands of Crete are guaranteed to be connected by the sea routes.

The following N−1N-1 lines describe the sea routes. Each line contains 33 integers each, u,vu, v (1≤u,v≤N,u≠v1 \leq u, v \leq N, u \neq v) and cc (1≤c≤1091 \leq c \leq 10^9), explaining that the sea route connects islands uu and vv and Agamemnon can acquire cc units of resources along this route. All sea routes are bidirectional, i.e. they can be used to travel from island uu to vv, or from island vv to uu.

Output

Output a single value, the maximum amount of resources Agamemnon can acquire with a feasible plan, as described in the statement.

Hint

There are 55 islands in Crete, connected to each other via 44 routes, as shown in the figure: the first connecting island 11 and 22 and allowing Agamemnon to acquire 33 units of resources and so on. In this archipelago, the best plan for Agamemnon is to start at island 44, visit island 11 (acquiring 55 units of resources along the 4→14\to1 route), and then end his path at island 55 (acquiring another 99 units of resources along the 1→51\to5 route), having earned a total of 1414 units of resources.

Examples2

  1. Example 1

    Input
    5 1
    1 2 3
    2 3 1
    1 4 5
    1 5 9
    
    Expected output
    14
    
  2. Example 2

    Input
    5 2
    1 2 3
    2 3 1
    1 4 5
    1 5 9
    
    Expected output
    18