import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline n, k = map(int, input().split()) g = [[] for i in range(n)] toc = {} edge = [] for i in range(n-1): a, b, c = map(int, input().split()) a, b = a-1, b-1 g[a].append(b) g[b].append(a) toc[(a, b)] = c toc[(b, a)] = c edge.append((a, b, c)) s = [] s.append(0) parent = [-1]*n order = [] while s: v = s.pop() order.append(v) for u in g[v]: if parent[v] != u: s.append(u) parent[u] = v order.reverse() leaf = [0]*n for v in order: if len(g[v]) == 1: leaf[v] = 1 p = parent[v] if p != -1: leaf[p] += leaf[v] dp1 = [0]*n for v in order: p = parent[v] if p != -1: dp1[p] += dp1[v]+toc[(p, v)]*leaf[v] #print(dp1) dp2 = [-1]*(k+1) dp2[0] = 0 for a, b, c in edge: if parent[a] == b: v = a else: v = b for i in reversed(range(k+1)): if dp2[i] == -1: continue if i+c <= k: dp2[i+c] = max(dp2[i+c], dp2[i]+c*leaf[v]) #print(dp2) ans = dp1[0]+max(dp2) print(ans)