import sys sys.setrecursionlimit(10**6) n,k = map(int,input().split()) ab = [[] for i in range(n)] val = [] for i in range(n-1): a,b,c = map(int,input().split()) a -= 1 b -= 1 ab[a].append([b,i]) ab[b].append([a,i]) val.append(c) li = [0]*(n-1) def dfs(v,pre,idx): tmp = 0 for i in range(len(ab[v])): if ab[v][i][0] != pre: #print(ab[v][i]+1) dfs(ab[v][i][0],v,ab[v][i][1]) tmp += li[ab[v][i][1]] #print(ab[v],len(ab[v]),"len") if i == len(ab[v])-1: tmp -= 1 tmp = max(0,tmp) #print(v+1) li[idx] = tmp+1 #print(li,idx) #return tmp+1 for v,idx in ab[0]: #print(v,idx) dfs(v,0,idx) #print(li,"li") dp = [[0]*(k+1) for _ in range(n)] ans = 0 for i in range(n-1): ans += val[i]*li[i] for i in range(n-1): for j in range(k+1): if j-val[i] >= 0: dp[i+1][j] = max(dp[i][j-val[i]]+val[i]*li[i],dp[i][j]) else: dp[i+1][j] = dp[i][j] #for i in range(k+1): # add = max(add,dp[n-1][i]) #print(dp) add = dp[n-1][k] print(add+ans)