/* -*- coding: utf-8 -*- * * 1488.cc: No.1488 Max Score of the Tree - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100; const int MAX_K = 100000; /* typedef */ typedef pair pii; typedef vector vpii; /* global variables */ int cs[MAX_N], ls[MAX_N], ews[MAX_N]; int ps[MAX_N], pes[MAX_N], ds[MAX_N], cis[MAX_N]; vpii nbrs[MAX_N]; int dp[MAX_K + 1]; /* subroutines */ inline void setmax(int &a, int b) { a = max(a, b); } /* main */ int main() { int n, k; scanf("%d%d", &n, &k); int m = n - 1; for (int i = 0; i < m; i++) { int a, b; scanf("%d%d%d", &a, &b, cs + i); a--, b--; nbrs[a].push_back(pii(b, i)); nbrs[b].push_back(pii(a, i)); } ps[0] = pes[0] = -1; ds[0] = 0; int dsum = 0; for (int u = 0; u >= 0;) { vpii &nbru = nbrs[u]; int up = ps[u]; if (cis[u] < nbru.size()) { pii pv = nbru[cis[u]++]; int v = pv.first, ei = pv.second; if (v != up) { ps[v] = u, pes[v] = ei; ds[v] = ds[u] + cs[ei]; u = v; } } else { if (u > 0 && nbru.size() == 1) { ls[u] = 1; dsum += ds[u]; } if (up >= 0) { ls[up] += ls[u]; ews[pes[u]] = cs[pes[u]] * ls[u]; } u = up; } } //for (int i = 0; i < n; i++) printf("%d ", ls[i]); putchar('\n'); memset(dp, -1, sizeof(dp)); dp[0] = 0; for (int i = 0; i < m; i++) for (int j = k - cs[i]; j >= 0; j--) if (dp[j] >= 0) setmax(dp[j + cs[i]], dp[j] + ews[i]); int maxd = 0; for (int j = 0; j <= k; j++) setmax(maxd, dp[j]); printf("%d\n", dsum + maxd); return 0; }