#include #include #include #define lint long long int using namespace std; const int NMAX = 100005; int n; vector > graph[NMAX]; int fathers[20][NMAX]; int costs[20][NMAX]; void dfs(int node) { for (int i = 1; i < 20; ++ i) { fathers[i][node] = fathers[i - 1][fathers[i - 1][node]]; costs[i][node] = costs[i - 1][node] + costs[i - 1][fathers[i - 1][node]]; } for (auto it: graph[node]) if (it.first != fathers[0][node]) { fathers[0][it.first] = node; costs[0][it.first] = it.second; dfs(it.first); } } int count(int node, int sum) { int ans = 0; for (int i = 19; i >= 0; -- i) if (fathers[i][node] && costs[i][node] <= sum) { sum -= costs[i][node]; node = fathers[i][node]; ans += (1 << i); } return ans; } lint count(int sum) { lint ans = 0; for (int i = 1; i <= n; ++ i) ans += count(i, sum); return ans; } int main() { ios_base :: sync_with_stdio(false); lint k; cin >> n >> k; int a, b ,c; for (int i = 1; i < n; ++ i) { cin >> a >> b >> c; graph[a].push_back({b, c}); graph[b].push_back({a, c}); } dfs(1); int st = 0; int dr = 1e9 + 5; int mid; int ans = 1e9 + 6; while (st <= dr) { mid = (st + dr) >> 1; if (count(mid) >= k) { ans = mid; dr = mid - 1; } else st = mid + 1; } if (ans == 1e9 + 6) cout << "-1\n"; else cout << ans << '\n'; return 0; }