#include #include #include

using namespace std;

struct edge { int t, v; edge *next; };

typedef pair <int, int> dpr; struct seg { dpr d; int l, r; seg *ls, *rs; };

struct ptrec { dpr d; int l, r, du; seg *rt; }; inline bool operator <(const ptrec& a, const ptrec& b) { return a. d. first < b. d. first; }

const int maxn = 50009; const int maxl = 17; const int maxbuf = maxn * maxl;

int n, m; int vis[maxn], tvis; int dd[maxn], df[maxl][maxn], dl[maxl][maxn], ds[maxl][maxn], *ar[maxn]; int ibuf_arr[maxbuf], *ibuf(ibuf_arr); edge ebuf_arr[maxn « 2], *ebuf(ebuf_arr), *head[maxn]; seg sbuf_arr[maxbuf * 3], *sbuf(sbuf_arr), *rt[maxn]; ptrec rhp[maxbuf + 300000]; int th;

inline void addEdge(int u, int v, int w) { ebuf-> t = v; ebuf-> v = w; ebuf-> next = head[u]; head[u] = ebuf ++; }

namespace tree_kit { int n, sz[maxn], d, ret, retv, dfo; inline void init(int no, int cd, int ci) { retv = (n = no) + 1; d = dl[cd]; dfo = ci; } void dfsCenter(int p) { int ms(1); vis[p] = tvis; sz[p] = 1; for (edge e = head[p]; e; e = e-> next) if (!dd[e-> t] && vis[e-> t] != tvis) { dfsCenter(e-> t); sz[p] += sz[e-> t]; ms = max(ms, sz[e-> t]); } ms = max(ms, n - sz[p]); if (ms < retv) retv = ms, ret = p; } void dfsDepth(int p, int fr) { if (!fr) d[p] = 0; (dfo ++) = d[p]; sz[p] = 1; for (edge e = head[p]; e; e = e-> next) if (!dd[e-> t] && e-> t != fr) { d[e-> t] = d[p] + e-> v; dfsDepth(e-> t, p); sz[p] += sz[e-> t]; } } };

#define midp ((p->l+p->r)»1) inline seg* sgtBuild(int l, int r, int* ar) { seg p(sbuf ++); p-> l = l; p-> r = r; if (l + 1 < r) { p-> ls = sgtBuild(l, midp, ar); p-> rs = sgtBuild(midp, r, ar); p-> d = max(p-> ls-> d, p-> rs-> d); } else p-> d = dpr(ar[l], l); return p; } inline dpr sgtQry(seg p, int l, int r) { if (p-> l == l && p-> r == r) return p-> d; else if (r <= midp) return sgtQry(p-> ls, l, r); else if (l >= midp) return sgtQry(p-> rs, l, r); else return max(sgtQry(p-> ls, l, midp), sgtQry(p-> rs, midp, r)); }

inline ptrec makeRec(int l, int r, int du, seg* rt) { ptrec ret; ret. l = l, ret. r = r; ret. d = sgtQry(rt, l, r); ret. d. first += (ret. du = du); ret. rt = rt; return ret; }

void divide(int ph, int dc, int sa) { ++ tvis; tree_kit :: init(sa, dc, ibuf); tree_kit :: dfsCenter(ph); int c(tree_kit :: ret); tree_kit :: dfsDepth(c, 0); dd[c] = dc; ar[c] = ibuf; ibuf += sa; int ci(0); for (edge* e = head[c]; e; e = e-> next) if (!dd[e-> t]) { ds[dc][ci] = tree_kit :: sz[e-> t]; divide(e-> t, dc + 1, ds[dc][ci]); ++ ci; } rt[c] = sgtBuild(0, sa, ar[c]); for (int i = 1, j = 0, csz = 1; i < sa; ++ i) { for (; j < ci && i >= csz + ds[dc][j]; ++ j) csz += ds[dc][j]; rhp[th ++] = makeRec(0, csz, ar[c][i], rt[c]); } }

int main() { #ifndef ONLINE_JUDGE freopen(“in.txt”, “r”, stdin); #endif

scanf("%d%d", &n, &m);
memset(head, 0, sizeof(head));
for (int i = 1; i < n; ++ i) {
	int u, v, w;
	scanf("%d%d%d", &u, &v, &w);
	addEdge(u, v, w);
	addEdge(v, u, w);
}
memset(dd, 0, sizeof(dd));
memset(vis, 0, sizeof(vis));
tvis = 0;
th = 0;
divide(1, 1, n);
make_heap(rhp, rhp + th);
while (m --) {
	ptrec g(rhp[0]);
	pop_heap(rhp, rhp + th --);
	printf("%d\n", g. d. first);
	int po(g. d. second);
	if (po > g. l) {
		rhp[th] = makeRec(g. l, po, g. du, g. rt);
		push_heap(rhp, rhp + ++ th);
	}
	if (po + 1 < g. r) {
		rhp[th] = makeRec(po + 1, g. r, g. du, g. rt);
		push_heap(rhp, rhp + ++ th);
	}
}

}