Under construction

Still being built. Things may change

Steppe on It

Time limit3sMemory limit1024 MB

Summary
Place f fire stations on a weighted tree of n villages to minimize the largest distance from any village to its nearest station.
Level

Medium6 of 10

Topics
Tree, Binary search, Greedy
Solved
No attempts yet

Problem

The gas pedal on the floor. Squealing tires. Wailing sirens. Emergency vehicles do whatever is necessary to reach their target locations as quickly as possible. Time is critical because lives often depend on it.

Providing emergency services is always challenging, especially for sparsely populated areas such as the Kazakh Steppe. The cost of building infrastructure is high compared to the number of people served. It is therefore important to minimize both the number of roads and the number of vehicles. On the other hand, it is also vital to minimize the response time of emergency services.

This problem considers a road network that already minimizes the number of roads, which means that any two villages are connected by exactly one path. Thanks to a government grant, the Kazakh Steppe Fire Department recently acquired some new fire engines. The department wants to establish fire stations in some of the villages and allocate the fire engines to them in a way that optimizes the guaranteed response time.

Your task is to find an optimal placement of fire engines that minimizes the time needed for any village to be reached by a fire engine. You can neglect the time needed to assemble the fire crew and start the engine as well as the time to travel across any villages. The response time is determined solely by traveling along the roads.

Input

The first line contains two integers: the number of villages nn (1≤n≤1000001 ≤ n ≤ 100000) and the number of fire engines ff (1≤f≤n1 ≤ f ≤ n).

This is followed by n−1n-1 lines numbered from 22 to nn. Line number ii contains two integers viv_i (1≤vi<i1 ≤ v_i < i) and tit_i (1≤ti≤100001 ≤ t_i ≤ 10000) meaning that there is a two-way road between villages ii and viv_i that can be traveled in time tit_i.

Output

Output the minimum response time that can be guaranteed by placing fire engines into ff villages.

Examples2

  1. Example 1

    Input
    6 2
    1 8
    2 7
    2 7
    3 6
    3 5
    
    Expected output
    8
    
  2. Example 2

    Input
    3 3
    1 1000
    2 1000
    
    Expected output
    0