N, K = map(int, input().split()) adj = [[] for i in range(N)] E = [] for i in range(N - 1): a, b, c = map(int, input().split()) a -= 1 b -= 1 adj[a].append((b, c)) adj[b].append((a, c)) E.append((a, b, c)) depth = [-1] * N depth[0] = 0 from collections import deque qu = deque([0]) while len(qu): v = qu.popleft() for nv, c in adj[v]: if depth[nv] == -1: depth[nv] = depth[v] + c qu.append(nv) dp = [1] * N for v in range(N): for nv, c in adj[v]: if depth[nv] > depth[v]: dp[v] = 0 break ans = sum([dp[i] * depth[i] for i in range(N)]) node = [i for i in range(N)] node.sort(key = lambda x: depth[x], reverse = True) for v in node: for nv, c in adj[v]: if depth[nv] < depth[v]: continue dp[v] += dp[nv] V = [] for i in range(N - 1): a, b, c = E[i] if depth[a] > depth[b]: a, b = b, a #aćŒęµ…ć„ V.append(c * dp[b]) dp = [[0] * (K + 1) for i in range(N)] for i in range(N - 1): for j in range(K + 1): dp[i + 1][j] = dp[i][j] if j + E[i][2] <= K: dp[i + 1][j] = max(dp[i + 1][j], dp[i][j + E[i][2]] + V[i]) print(max(dp[-1]) + ans)