import sys sys.setrecursionlimit(10**8) import pypyjit pypyjit.set_param("max_unroll_recursion=-1") input = sys.stdin.readline N, K = map(int, input().split()) G = [list() for _ in range(N)] for i in range(N-1): a, b, c = map(int, input().split()) a-=1;b-=1 G[a].append((b,c)) G[b].append((a,c)) leaf = [0]*N A = [] score = 0 def dfs(v, p): for next_v,_ in G[v]: if next_v == p: continue dfs(next_v, v) if len(G[v])==1 and v!=0: leaf[v] = 1 else: leaf[v] = 0 for c,cost in G[v]: if c== p: continue leaf[v] += leaf[c] A.append([cost,leaf[c]*cost]) global score score += cost*leaf[c] dfs(0,-1) dp = [0]*(K+1) for i in range(N-1): ndp = [0]*(K+1) w, v = A[i] for j in range(K+1): ndp[j] = max(ndp[j],dp[j]) if j-w>=0: ndp[j] = max(ndp[j],dp[j-w]+v) dp = ndp print(score+dp[-1])