#include #define rep(i,n) for(int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main(){ cin.tie(0); ios::sync_with_stdio(0); // (w,v) = (c(e), leaf_cnt(e.c) * c(e)) int N,K; cin >> N >> K; vector>> G(N); rep(i,N-1) { int a,b,c; cin >> a >> b >> c; a--; b--; G[a].push_back({b, c}); G[b].push_back({a, c}); } int ROOT = 0, SUM = 0; vector leaf_cnt(N, 0); vector> wv; function get_leaf = [&](int v, int p) -> void { int is_leaf = 1; for(auto [to, c] : G[v]) { if(to != p) { is_leaf = 0; get_leaf(to, v); leaf_cnt[v] += leaf_cnt[to]; } } leaf_cnt[v] += is_leaf; for(auto [to, c] : G[v]) { if(to != p) { wv.push_back({c, leaf_cnt[to] * c}); SUM += leaf_cnt[to] * c; } } }; get_leaf(ROOT, -1); vector dp(K + 1, 0); for(auto [w, v] : wv) { vector nt = dp; for(int x = 0; x <= K; x++) if(x + w <= K) nt[x + w] = max(nt[x + w], dp[x] + v); swap(dp, nt); } cout << SUM + *max_element(dp.begin(), dp.end()) << endl; }