#include using namespace std; const int BS = 8000; const int MAXN = 100005; int Val[MAXN], B[MAXN], E[MAXN], Sort[MAXN], Dist[MAXN], Size[MAXN]; int now = 0, n; long long total_paths = 0; vector> G[MAXN]; void dfs(int node) { now += 1; Val[now] = Dist[node]; B[node] = now; Size[node] = 1; for(auto vec : G[node]) { if(!B[vec.first]) { Dist[vec.first] = Dist[node] + vec.second; dfs(vec.first); Size[node] += Size[vec.first]; } } E[node] = now; total_paths += Size[node] - 1; } void preprocess() { for(int i = 1; i <= now; ++i) { Sort[i] = Val[i]; if(i % BS == 0) { sort(Sort + i - BS, Sort + i); } } } int query(int l, int r, int k) { int ans = 0; for(int i = l; i <= r;) { ans += (Val[i] <= k); i += 1; } return ans; } long long solve(int k) { long long ans = 0; for(int i = 1; i <= n; ++i) { if(k + Dist[i] < 0) ans += E[i] - B[i]; else ans += query(B[i] + 1, E[i], k + Dist[i]); } return ans; } int32_t main() { int a, b, c; long long m; cin >> n >> m; for(int i = 2; i <= n; ++i) { cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } dfs(1); preprocess(); if(m > total_paths) { cout << -1; return 0; } int ans = -1; for(int step = (1 << 29); step; step >>= 1) { if(solve(step + ans) < m) ans += step; } cout << ans + 1; return 0; }