N, K = map(int, input().split()) v = [[[], []] for _ in range(N)] weight = [] for i in range(N-1): a, b, c = map(int, input().split()) v[a-1][1].append((b-1, i)) v[b-1][1].append((a-1, i)) weight.append(c) q = [(0, [])] e = [0] * (N-1) while len(q) > 0: i, c = q.pop(0) v[i][0] = c cnt = True for j, k in v[i][1]: if j == 0 or len(v[j][0]) > 0: continue cnt = False q.append((j, c+[k])) if cnt: for j in c: e[j] += 1 dp = [[0] * (K+1)] for i in range(N-1): dp.append([max(dp[i][w-weight[i]] + weight[i] * e[i] * 2, dp[i][w] + weight[i] * e[i]) if w >= weight[i] else (dp[i][w] + weight[i] * e[i]) for w in range(K+1)]) print(dp[N-1][K])