/*
Author: Cadocx
Codeforces: https://codeforces.com/profile/Kadoc
VNOJ: oj.vnoi.info/user/Cadoc
*/
#include <bits/stdc++.h>
using namespace std;
// input/output
#define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define el cout << '\n'
//data type
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define vi vector<int>
#define vl vector<ll>
#define vc vector<char>
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; }
//STL
#define sz(x) (int)(x).size()
#define FOR(i,l,r) for(auto i = l; i <= r; i++)
#define FORD(i,r,l) for(auto i = r; i >= l; i--)
#define forin(i,a) for(auto i : a)
#define pb push_back
#define eb emplace_back
#define pf push_front
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
//bitmask
#define bitcnt(n) __builtin_popcount(n)
#define mask(i) (1 << (i))
#define bit(n, i) (((n) >> (i)) & 1)
#define set_on(n, i) ((n) | mask(i))
#define set_off(n, i) ((n) & ~mask(i))
//constant
#define maxN 200005
#define MOD 1000230007
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
#define base 31
#define Kadoc 0
int n, k;
int H[maxN][2], L[maxN];
int pos[maxN], p[maxN << 2][22], Timer = 0;
int h[maxN], d[maxN];
int sz[maxN], del[maxN];
vector<pii> g[maxN];
int f[1000005];
vector<int> V;
int Ans = INF;
void dfs(int u, int pr = -1){
pos[u] = ++Timer;
p[Timer][0] = u;
for(auto [v, w]:g[u]) if(v != pr){
h[v] = h[u] + 1;
d[v] = d[u] + w;
dfs(v, u);
p[++Timer][0] = u;
}
}
void dfs_sz(int u, int p = -1){
sz[u] = 1;
for(auto [v, w]:g[u]) if(v != p && !del[v]){
dfs_sz(v, u);
sz[u] += sz[v];
}
}
int getMin(int u, int v){
return (h[u] < h[v]? u:v);
}
int lca(int u, int v){
int l = pos[u], r = pos[v];
if(l > r) swap(l, r);
int k = 31 - __builtin_clz(r-l+1);
return getMin(p[l][k], p[r-mask(k)+1][k]);
}
int dist(int u, int v){
return h[u] + h[v] - 2 * h[lca(u, v)];
}
int distW(int u, int v){
return d[u] + d[v] - 2 * d[lca(u, v)];
}
void upd(int u, int root, int p = -1){
int hu = dist(u, root), du = distW(u, root);
if(k >= du){
minimize(f[du], hu);
V.pb(du);
}
for(auto [v, w]:g[u]) if(v != p && !del[v]) upd(v, root, u);
}
void get(int u, int root, int p = -1){
int hu = dist(u, root), du = distW(u, root);
if(k >= du) minimize(Ans, hu + f[k - du]);
for(auto [v, w]:g[u]) if(v != p && !del[v]) get(v, root, u);
}
int centroid(int u, int p, int n){
for(auto [v, w]:g[u]) if(v != p && !del[v] && sz[v] > n/2) return centroid(v, u, n);
return u;
}
void cd(int u, int p = -1){
dfs_sz(u);
u = centroid(u, p, sz[u]);
del[u] = 1;
f[0] = 0;
for(auto [v, w]:g[u]) if(!del[v]){
get(v, u);
upd(v, u);
}
for(int depth:V) f[depth] = INF;
V.clear();
for(auto [v, w]:g[u]) if(!del[v]) cd(v, u);
}
int best_path(int N, int K, int H[][2], int L[]){
n = N; k = K;
FOR(i, 0, n-2){
int u = H[i][0], v = H[i][1], w = L[i];
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(1);
for(int j=1; mask(j)<=Timer; ++j) for(int i=1; i+mask(j)-1<=Timer; ++i){
p[i][j] = getMin(p[i][j-1], p[i+mask(j-1)][j-1]);
}
memset(f, 0x3f, sizeof f);
cd(1);
if(Ans >= INF) Ans = -1;
return Ans;
}
int main(){
cin >> n >> k;
FOR(i, 0, n-2){
int u, v, w; cin >> u >> v >> w;
H[i][0] = ++u;
H[i][1] = ++v;
L[i] = w;
}
cout << best_path(n, k, H, L);
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccSDQ6KS.o: in function `main':
race.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cc606dwU.o:grader.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status