n,k = map(int, input().split()) G = [[] 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)) check = [0]*n check[0] = 1 stack = [0] for i in range(n-1): for to,cost in G[stack[i]]: if check[to]: continue check[to] = 1 stack.append(to) dist = [[0,0] for _ in range(n)] for i in range(n-1, -1, -1): check[stack[i]] = 0 ex = 1 for to,cost in G[stack[i]]: if check[to] == 1: continue ex = 0 dist[stack[i]][0] += dist[to][0] dist[stack[i]][1] += dist[to][1] + dist[to][0]*cost dist[stack[i]][0] += ex dp = [0]*(k+1) for i in range(n): check[stack[i]] = 1 for to,cost in G[stack[i]]: if check[to]: continue for j in range(k, cost-1, -1): if dp[j] < dp[j-cost] + cost*dist[to][0]: dp[j] = dp[j-cost] + cost*dist[to][0] print(dist[0][1] + max(dp))