import sys input = sys.stdin.readline N,K=map(int,input().split()) E=[tuple(map(int,input().split())) for i in range(N-1)] EDGE=[[] for i in range(N)] for a,b,e in E: a-=1 b-=1 EDGE[a].append((b,e)) EDGE[b].append((a,e)) QUE=[0] Parent=[-1]*(N+1) Parent[0]=0 TOP_SORT=[] while QUE: x=QUE.pop() TOP_SORT.append(x) for to,w in EDGE[x]: if Parent[to]==-1: Parent[to]=x,w QUE.append(to) EW=[0]*N for n in TOP_SORT[1:][::-1]: if EW[n]==0: EW[n]=1 EW[Parent[n][0]]+=EW[n] XX=[] for i in range(1,N): XX.append((Parent[i][1],EW[i])) ANS=0 for x,c in XX: ANS+=x*c cost=0 DP=[0]*(K+1) for x,c in XX: for j in range(K,-1,-1): if j+x<=K: DP[j+x]=max(DP[j+x],DP[j]+x*c) print(max(DP)+ANS)