// tarsasag.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <queue>
#include <deque>
#include <algorithm>
#include <cmath>
using namespace std;
struct adat
{
int lat;
int keses,delay;
deque<pair < int, int >> sz;
};
deque<adat>x;
priority_queue<int>p;
void melysegi(int csp)
{
x[csp].lat = 1;
for (auto& e : x[csp].sz)
{
if (!x[e.first].lat)
{
if (x[e.first].sz.size()>1)melysegi(e.first);
else
{
x[e.first].keses = e.second;
p.push(e.second);
}
if (csp != 1)
{
x[csp].keses = max(x[csp].keses,x[e.first].keses+x[csp].delay);
}
}
}
if(csp!=1)p.push(x[csp].keses);
}
int main()
{
int n, k;
cin >> n >> k;
x.resize(n + 1);
for (int i = 1; i <= n - 1; ++i)
{
int a, b;
cin >> a >> b;
x[i + 1].sz.push_back({ a,b });
x[i + 1].delay = b;
x[a].sz.push_back({ i + 1,b });
}
melysegi(1);
for (int i = 1; i <= k; ++i)
{
p.pop();
}
cout << p.top();
return 0;
}
/*
5 1
1 5
1 2
2 4
2 2
*/
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file