import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() inf = 10 ** 16 md = 10 ** 9 + 7 # md = 998244353 n,k=LI() to=[[] for _ in range(n)] cc=[] for i in range(n-1): u,v,c=LI() u,v=u-1,v-1 to[u].append((v,c,i)) to[v].append((u,c,i)) cc.append(c) leaf=[0]*n etou=[0]*n def dfs(u=0,pu=-1,s=0): if u and len(to[u])==1: leaf[u]=1 return s res=0 for v,c,e in to[u]: if v==pu:continue etou[e]=v res+=dfs(v,u,s+c) leaf[u]+=leaf[v] return res score=dfs() dp=[-1]*(k+1) dp[0]=0 for e in range(n-1): c=cc[e] u=etou[e] for i in range(k-c,-1,-1): if dp[i]==-1:continue ni=i+c dp[ni]=max(dp[ni],dp[i]+c*leaf[u]) print(score+max(dp))