5 10 1 2 2 2 3 2 2 5 3 3 4 3 1 2 3 4 5
11
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define INF 0x3f3f3f3f 15 using namespace std; 16 const int maxn = 110; 17 struct arc { 18 int to,w; 19 }; 20 int dp[maxn][510],val[maxn],n,t,sum; 21 vector<arc>g[maxn]; 22 bool dfs1(int u, int fa){ 23 if(u == n) return true; 24 for(int i = 0; i < g[u].size(); i++){ 25 if(g[u][i].to == fa) continue; 26 if(dfs1(g[u][i].to,u)){ 27 sum += g[u][i].w; 28 g[u][i].w = 0; 29 return true; 30 } 31 } 32 return false; 33 } 34 void dfs(int u,int fa) { 35 for(int i = 0; i <= t; i++) dp[u][i] = val[u]; 36 for(int v = 0; v < g[u].size(); v++) { 37 if(g[u][v].to == fa) continue; 38 dfs(g[u][v].to,u); 39 int cost = 2*g[u][v].w; 40 for(int j = t; j >= cost; j--){ 41 for(int k = 0; k <= j - cost; k++) 42 dp[u][j] = max(dp[u][j],dp[u][j-k-cost]+dp[g[u][v].to][k]); 43 } 44 } 45 } 46 int main() { 47 int u,v,w,i; 48 while(~scanf("%d %d",&n,&t)) { 49 for(i = 0; i <= n; i++) 50 g[i].clear(); 51 for(i = 1; i < n; i++) { 52 scanf("%d %d %d",&u,&v,&w); 53 g[u].push_back((arc) {v,w}); 54 g[v].push_back((arc) {u,w}); 55 } 56 for(i = 1; i <= n; i++) 57 scanf("%d",val+i); 58 memset(dp,0,sizeof(dp)); 59 sum = 0; 60 dfs1(1,-1); 61 if(sum > t){ 62 puts("Human beings die in pursuit of wealth, and birds die in pursuit of food!"); 63 continue; 64 } 65 t -= sum; 66 dfs(1,-1); 67 cout<<dp[1][t]<<endl; 68 } 69 return 0; 70 }