#include using namespace std; typedef long long ll; constexpr int Inf = 2000000030; constexpr ll INF= 2000000000000000001; constexpr ll MOD = 1000000007; const double PI = 3.14159265358979323846; typedef pair P; typedef pair PP; template vector make_vector(size_t s) { return vector(s); } template auto make_vector(size_t s,Args... args) { return vector(s,make_vector(args...)); } template inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } ll mod(ll val, ll M) { val = val % M; if(val < 0) { val += M; } return val; } template T RS(T N, T P, T M){ if(P == 0) { return 1; } if(P < 0) { return 0; } if(P % 2 == 0){ ll t = RS(N, P/2, M); if(M == -1) return t * t; return t * t % M; } if(M == -1) { return N * RS(N,P - 1,M); } return N * RS(N, P-1, M) % M; } int N,K; vector

vec; vector cost; vector graph[110]; vector rgraph; vector cnt; queue leaf; ll dp[110][100010]; void bfs() { vector used(N); queue que; que.push(0); while(!que.empty()) { int p = que.front(); que.pop(); used.at(p) = true; bool isleaf = true; for(auto x:graph[p]) { if(!used.at(x)) { que.push(x); rgraph.at(x) = p; isleaf = false; } } if(isleaf) leaf.push(p); } return; } void bfs2() { cnt.resize(N,0); while(!leaf.empty()) { int p = leaf.front(); leaf.pop(); while(p != -1) { cnt.at(p)++; p = rgraph.at(p); } } return; } int main() { cin >> N >> K; vec.resize(N - 1); cost.resize(N - 1); rgraph.resize(N); rgraph.at(0) = -1; for(int i = 0;i < N - 1;i++) { int a,b,c; cin >> a >> b >> c; a--; b--; vec.at(i) = {a,b}; cost.at(i) = c; graph[a].push_back(b); graph[b].push_back(a); } bfs(); bfs2(); ll Sum = 0; vector

vec2(N - 1); for(int i = 0;i < N - 1;i++) { vec2.at(i) = {min(cnt.at(vec.at(i).first),cnt.at(vec.at(i).second)) * cost.at(i),cost.at(i)}; Sum += min(cnt.at(vec.at(i).first),cnt.at(vec.at(i).second)) * cost.at(i); } for(int i = 0;i < N;i++) { for(int j = 0;j <= K;j++) { dp[i][j] = 0; } } for(int i = 0;i < N - 1;i++) { for(int j = 0;j <= K;j++) { if(j < vec2.at(i).second) { dp[i + 1][j] = dp[i][j]; } else { dp[i + 1][j] = max(dp[i][j],dp[i][j - vec2.at(i).second] + vec2.at(i).first); } } } cout << Sum + dp[N - 1][K] << endl; }