import sys def input(): return sys.stdin.readline().rstrip() """ Do not get stuck on a problem for more than 20 minutes Just check the editorial :) There should be something else to do insead """ INF = 1 << 128 def slv(): n, k = map(int, input().split()) g = [[]for i in range(n)] edge = [] for i in range(n - 1): u, v, c = map(int, input().split()) u -= 1 v -= 1 g[u].append((v,c)) g[v].append((u,c)) edge.append((u, v, c)) visited = [INF]*(n) cnt = [0]*(n) par = [-1]*(n) ans = 0 def dfs(S): if visited[S] == INF: visited[S] = 0 for adj,c in g[S]: if visited[adj] == INF: par[adj] = S visited[adj] = visited[S] + c dfs(adj) if all(visited[u] < visited[S] for u,c in g[S]): cnt[S] = 1 if par[S] >= 0: cnt[par[S]] += cnt[S] dfs(0) for i in range(n): if all(visited[u] < visited[i] for u,c in g[i]): ans += visited[i] data = [] for u, v, c in edge: if visited[u] < visited[v]: data.append((cnt[v], c)) else: data.append((cnt[u], c)) dp = [[-INF]*(k + 1) for i in range(n)] dp[0][0] = 0 for i in range(1,n): cnt,cost = data[i - 1] V,cost = cnt * cost,cost for j in range(k + 1): if j + cost <= k: dp[i][j + cost] = max(dp[i][j + cost],dp[i - 1][j] + V) dp[i][j] = max(dp[i][j],dp[i - 1][j]) ans += max(dp[n - 1][c] for c in range(k + 1)) print(ans) return def main(): t = 1 for i in range(t): slv() return if __name__ == "__main__": main()