#include #include #include #include using namespace std; const int MAXN = 100005; int Lin[2*MAXN], Dist[MAXN], Size[MAXN], I[MAXN], Pos[MAXN]; int now = 0, n; long long total_paths = 0; vector> G[MAXN]; auto cmp = [](int a, int b) { return Dist[a] < Dist[b]; }; void dfs(int node) { Lin[++now] = node; Size[node] = 1; for(auto vec : G[node]) { if(!Size[vec.first]) { Dist[vec.first] = Dist[node] + vec.second; dfs(vec.first); Size[node] += Size[vec.first]; } } Lin[++now] = -node; total_paths += Size[node] - 1; } int T[MAXN]; void add(int pos) { for(; pos <= n; pos += (pos & -pos)) ++T[pos]; } int query(int x) { Dist[n + 1] = x; int pos = upper_bound(I + 1, I + n + 1, n + 1, cmp) - I - 1; int s = 0; for(; pos; pos -= (pos & -pos)) s += T[pos]; return s; } long long solve(int k) { long long ans = 0; memset(T, 0, sizeof(T)); for(int i = 1; i <= 2 * n; ++i) { if(Lin[i] > 0) { add(Pos[Lin[i]]); ans -= query(Dist[Lin[i]] + k); } else { ans += query(Dist[-Lin[i]] + k); } } return ans; } void Read(int &a) { char c; for(c = getchar(); !isdigit(c); c = getchar()); for(a = 0; isdigit(c); c = getchar()) a = (a << 1) + (a << 3) + c - '0'; } int32_t main() { int a, b, c; long long m; cin >> n >> m; for(int i = 2; i <= n; ++i) { Read(a); Read(b); Read(c); G[a].push_back({b, c}); G[b].push_back({a, c}); } dfs(1); if(m > total_paths) { cout << -1; return 0; } for(int i = 1; i <= n; ++i) I[i] = i; sort(I + 1, I + n + 1, cmp); for(int i = 1; i <= n; ++i) Pos[I[i]] = i; int ans = -1; for(int step = (1 << 29); step; step >>= 1) { if(solve(step + ans) < m) ans += step; } cout << ans + 1; return 0; }