#include typedef struct List { struct List *next; int v, cost; } list; void chmax(int* a, int b) { if (*a < b) *a = b; } int main() { int i, N, K, u, w, c; list *adj[101] = {}, e[201], *p; scanf("%d %d", &N, &K); for (i = 0; i < N - 1; i++) { scanf("%d %d %d", &u, &w, &c); e[i*2].v = w; e[i*2+1].v = u; e[i*2].cost = c; e[i*2+1].cost = c; e[i*2].next = adj[u]; e[i*2+1].next = adj[w]; adj[u] = &(e[i*2]); adj[w] = &(e[i*2+1]); } int par[101] = {}, depth[101], q[101], head, tail; par[1] = 1; depth[1] = 0; q[0] = 1; for (head = 0, tail = 1; head < tail; head++) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; if (par[w] == 0) { par[w] = u; depth[w] = depth[u] + p->cost; q[tail++] = w; } } } int leaf[101] = {}, weight[101], value[101], sum = 0; for (head--; head >= 0; head--) { u = q[head]; for (p = adj[u]; p != NULL; p = p->next) { w = p->v; c = p->cost; if (w == par[u]) continue; leaf[u] += leaf[w]; weight[w] = c; value[w] = leaf[w] * c; } if (leaf[u] == 0) { sum += depth[u]; leaf[u] = 1; } } int ans = 0, dp[100001] = {}; for (u = 2; u <= N; u++) { for (i = K; i >= weight[u]; i--) chmax(&(dp[i]), dp[i - weight[u]] + value[u]); } for (i = 0; i <= K; i++) chmax(&ans, dp[i]); printf("%d\n", ans + sum); fflush(stdout); return 0; }