#pragma region #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace atcoder; using mint = modint1000000007; //using mint = modint998244353; //using mint = modint; #pragma region using using namespace std; typedef long long ll; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using vmint = vector; using vvmint = vector; using pint = pair; using pll = pair; using vpint = vector; using vvpint = vector; //#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i)) #define rep(i, e) for (int(i) = 0; (i) < (e); ++(i)) #define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i)) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #pragma endregion #pragma region UnionFind struct UnionFind { vector par; UnionFind(int n) : par(n, -1) {} void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; #pragma endregion #pragma region GCD ll gcd(ll a, ll b) { if (b == 0)return a; return gcd(b, a%b); } #pragma endregion #pragma region LCM ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } #pragma endregion #pragma region chmin template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } #pragma endregion #pragma region chmax template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } #pragma endregion #pragma region Dijkstra vl dijkstra(vector>> v, int s) { ll INF = 1e18; int MAX = 1e6; vl res(MAX, INF); priority_queue, vector>, greater>> q; q.push({ 0,s }); while (!q.empty()) { int now; ll d; tie(d, now) = q.top(); q.pop(); if (!chmin(res[now], d))continue; for (auto p : v[now]) { int next; ll c; tie(next, c) = p; if (res[next] <= res[now] + c)continue; q.push({ res[now] + c,next }); } } return res; } #pragma endregion //グリッド内チェック bool out(int x, int y, int h, int w) { if (x < 0 || h <= x || y < 0 || w <= y)return true; else return false; } #pragma endregion vvi v(101); vi cnt(101); int s = 0; vvpint v2(101); vi d(101); struct edge { int a; int b; int c; }; int dfs(int now, int p) { bool f = true; for (int next : v[now]) { if (next == p)continue; cnt[now] += dfs(next, now); f = false; } if (f)cnt[now] = 1; return cnt[now]; } void dfs2(int now, int p, int sum) { d[now] = sum; bool f = true; for (auto q : v2[now]) { int next, l; tie(next, l) = q; if (next == p)continue; dfs2(next, now, sum + l); f = false; } if (f)s += d[now]; } int main() { int n, k; cin >> n >> k; vector e(n - 1); rep(i, n - 1) { int a, b, c; cin >> a >> b >> c; --a, --b; v[a].push_back(b); v[b].push_back(a); e[i] = { a,b,c }; v2[a].push_back({ b,c }); v2[b].push_back({ a,c }); } dfs(0, -1); dfs2(0, -1, 0); vi w(n - 1), val(n - 1); rep(i, n - 1) { auto t = e[i]; int a = t.a, b = t.b, c = t.c; w[i] = c, val[i] = c * min(cnt[a], cnt[b]); } vvi dp(n, vi(k + 1)); rep(i, n - 1)rep(j, k + 1) { chmax(dp[i + 1][j], dp[i][j]); if (j + w[i] <= k)chmax(dp[i + 1][j + w[i]], dp[i][j] + val[i]); } cout << s + *max_element(all(dp[n - 1])) << endl; }