N,K = map(int,input().split()) G = [[] for _ in range(N + 1)] for _ in range(N - 1): a,b,c = map(int,input().split()) G[a].append((b,c)) G[b].append((a,c)) node = [[0] * 2 for _ in range(N + 1)] edge = [] score = 0 def tansaku(now = 1,parent = 0): global score node[now][0] = parent if now != 1 and len(G[now]) == 1: node[now][1] = 1 return 1 for v,c in G[now]: if v != parent: t = tansaku(v,now) node[now][1] += t edge.append((c,t)) score += c * t return node[now][1] tansaku() dp = [[0] * (K + 1) for _ in range(N-1)] dp[0][edge[0][0]] = edge[0][1] * edge[0][0] for i in range(1,N-1): c,t = edge[i] for j in range(K + 1): dp[i][j] = dp[i-1][j] if j - c >= 0: if dp[i][j] < dp[i-1][j-c] + c * t: dp[i][j] = dp[i-1][j-c] + c * t m = max(dp[N-2]) print(m + score)