from sys import stdin readline = stdin.readline N, K = map(int, readline().split()) links = [{} for _ in range(N)] for _ in range(N - 1): a, b, c = map(int, readline().split()) a, b = a - 1, b - 1 links[a][b] = c links[b][a] = c def dfs1(n): l = links[n] for x in l: del links[x][n] for x in l: dfs1(x) dfs1(0) result = 0 t = [] def dfs2(n): global result l = links[n] if len(l) == 0: return 1 c = 0 for x in l: a = dfs2(x) c += a t.append((l[x], a)) result += l[x] * a return c dfs2(0) dp = [-1] * (K + 1) dp[0] = 0 for i in range(len(t)): for j in range(K - 1, -1, -1): if dp[j] == -1: continue a = j + t[i][0] if a > K: continue dp[a] = max(dp[a], dp[j] + t[i][0] * t[i][1]) result += max(dp) print(result)