using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var p = NList; var (n, k) = (p[0], p[1]); var map = NArr(n - 1); var tree = new List<(int id, int to)>[n]; for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int id, int to)>(); for (var i = 0; i < map.Length; ++i) { tree[map[i][0] - 1].Add((i, map[i][1] - 1)); tree[map[i][1] - 1].Add((i, map[i][0] - 1)); } var mul = new int[n - 1]; DFS(0, -1, tree, mul); var dp = new long[k + 1]; var ans = 0L; for (var i = 0; i < map.Length; ++i) { ans += map[i][2] * mul[i]; var ndp = (long[]) dp.Clone(); for (var j = 0; j < dp.Length && j + map[i][2] <= k; ++j) { ndp[j + map[i][2]] = Math.Max(ndp[j + map[i][2]], dp[j] + map[i][2] * mul[i]); } dp = ndp; } WriteLine(ans + dp.Max()); } static int DFS(int cur, int prev, List<(int id, int to)>[] tree, int[] mul) { var count = 0; foreach (var next in tree[cur]) { if (next.to == prev) continue; var sub = DFS(next.to, cur, tree, mul); mul[next.id] = sub; count += sub; } if (count == 0) ++count; return count; } }