import sys from collections import deque input = sys.stdin.buffer.readline N, K = map(int, input().split()) edges = [] T = [[] for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, input().split()) a -= 1 b -= 1 edges.append((a, b, c)) T[a].append((b, c)) T[b].append((a, c)) dist = [None] * N dist[0] = 0 d = deque([0]) dfs_order = [] is_leaf = [True] * N while d: v = d.pop() dv = dist[v] for x, c in T[v]: if dist[x] is None: dfs_order.append((v, x)) dist[x] = dv + c d.append(x) is_leaf[v] = False desc_leaves = [int(l) for l in is_leaf] for v, x in reversed(dfs_order): desc_leaves[v] += desc_leaves[x] dp = [-10 ** 9] * (K + 1) dp[0] = 0 for a, b, c in edges: new_dp = dp.copy() value = c * (min(desc_leaves[a], desc_leaves[b])) for i in range(K + 1 - c): new_dp[i + c] = max(new_dp[i + c], dp[i] + value) dp = new_dp print(max(dp) + sum(ds * l for ds, l in zip(dist, is_leaf)))