#include #define N 110 #define K 100100 int pnt[N], G[N][N][2]; int dist[N]; int dp[K]; int dfs(int i, int j) { int res = 0; for(int k = 0; k < pnt[i]; k++) { if(G[i][k][0] == j) continue; int u = dfs(G[i][k][0], i); res += u + dist[G[i][k][0]]*G[i][k][1]; dist[i] += dist[G[i][k][0]]; for(int l = K-1; l >= G[i][k][1]; l--) { if(dp[l] < dp[l-G[i][k][1]] + G[i][k][1]*dist[G[i][k][0]]) { dp[l] = dp[l-G[i][k][1]] + G[i][k][1]*dist[G[i][k][0]]; } } } dist[i] += !res; return res; } int main() { int n, k; scanf("%d%d", &n, &k); for(int i = 0; i < n-1; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--; b--; G[a][pnt[a]][0] = b; G[a][pnt[a]++][1] = c; G[b][pnt[b]][0] = a; G[b][pnt[b]++][1] = c; } int ans = dfs(0, -1); int max = 0; for(int i = 0; i <= k; i++) if(max < dp[i]) max = dp[i]; printf("%d\n", ans+max); }