Under construction

Still being built. Things may change

KK Subway Stations

Time limit1.5sMemory limit1024 MB

Summary
Given a weighted tree, choose a path of at most K nodes that minimizes the maximum distance from any node to the path.
Level

Hard8 of 10

Topics
Tree, Binary search, Dynamic programming, DFS
Solved
No attempts yet

Problem

Seoul City Mayor Hanbyeol plans to build a subway system to ease traffic congestion on the city's crowded roads. The budget allows at most KK stations, and all stations must be connected by a single path of roads.

Seoul is modeled as a connected graph with NN nodes and N−1N-1 bidirectional edges, where each node is a building and each edge is a road. Hanbyeol must choose a simple path of XX nodes, where 1≤X≤K1 \le X \le K, and build a station on every node along that path.

Hanbyeol wants to minimize the maximum distance from any building to its nearest station. Write a program that finds the station placement that minimizes this value.

Input

The first line contains two space-separated integers NN, the number of nodes in the city, and KK, the maximum number of subway stations Hanbyeol can build. (1≤K≤N≤300 0001 \le K \le N \le 300\,000)

Each of the next N−1N-1 lines contains three space-separated integers uiu_i, viv_i, and lil_i. These mean that the ii-th road connects nodes uiu_i and viv_i, and its length is lil_i kilometers. (1≤ui,vi≤N1 \le u_i, v_i \le N; 1≤li≤10121 \le l_i \le 10^{12}; if i≠ji \ne j, then {ui,vi}≠{uj,vj}\{u_i,v_i\} \ne \{u_j,v_j\})

Output

Print the maximum distance from any building to its nearest station when this value is minimized, in kilometers.

Hint

(For Sogang students.) This problem is an adapted version that matches the format of a problem in a general programming contest. In the exam, the original scoring was:

  • 55 points for Subtask 1.
  • 40 points for Subtask 2.
  • 3 points for Subtask 3.
  • 2 points for Subtask 4.

Examples2

  1. Example 1

    Input
    9 3
    1 2 5
    2 3 8
    3 4 6
    3 5 9
    5 6 8
    5 9 11
    6 7 1
    6 8 12
    
    Expected output
    13
    
  2. Example 2

    Input
    9 5
    1 2 5
    2 3 8
    3 4 6
    3 5 9
    5 6 8
    5 9 11
    6 7 1
    6 8 12
    
    Expected output
    11