from collections import deque N, K = map(int, input().split()) X = [[] for i in range(N)] for i in range(N-1): x, y, c= map(int, input().split()) x, y = x-1, y-1 # 1-indexed X[x].append((y, c)) X[y].append((x, c)) P = [-1] * N Q = deque([0]) R = [] S = [0] * N while Q: i = deque.popleft(Q) R.append(i) for a, c in X[i]: if a != P[i]: P[a] = i S[a] = c X[a].remove((i, c)) deque.append(Q, a) T = [0] * N ans = 0 Y = [] for i in R[1:][::-1]: if not len(X[i]): T[i] += 1 Y.append((S[i], S[i] * T[i])) ans += S[i] * T[i] T[P[i]] += T[i] Z = [0] * (K + 1) for y, v in Y: for i in range(K, y-1, -1): Z[i] = max(Z[i], Z[i-y] + v) ans += Z[K] print(ans)