Problem Description
To kill all the bugs is always easier than to capture their brains. A map is drawn for you, with all the rooms marked by the amount of bugs inside, and the possibility of containing a brain. The cavern's structure is like a tree in such a way that there is one unique path leading to each room from the entrance. To finish the battle as soon as possible, you do not want to wait for the troopers to clear a room before advancing to the next one, instead you have to leave some troopers at each room passed to fight all the bugs inside. The troopers never re-enter a room where they have visited before.
A starship trooper can fight against 20 bugs. Since you do not have enough troopers, you can only take some of the rooms and let the nerve gas do the rest of the job. At the mean time, you should maximize the possibility of capturing a brain. To simplify the problem, just maximize the sum of all the possibilities of containing brains for the taken rooms. Making such a plan is a difficult job. You need the help of a computer.
Input
The last test case is followed by two -1's.
Output
Sample Input
Sample Output
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <map>
using namespace std;
typedef long long ll;
const double inf=1e20;
const int maxn=+;
const int maxm=+; vector<ll>edges[maxn]; ll f[maxn][maxm]; ll n,m;
ll v[maxn],c[maxn]; void addedge(ll u,ll v){
edges[u].push_back(v);
edges[v].push_back(u); } void clear_(ll n){
for(ll i=;i<=n;i++)edges[i].clear();
memset(f,,sizeof(f));
} void dp(ll x,ll fa){
for(ll i=;i<(ll)edges[x].size();i++){
ll y=edges[x][i];
if(y!=fa){
dp(y,x);
for(ll t=m;t>=;t--){
for(ll j=t;j>=;j--){
if(t-j>=){
f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
}
}
}
}
}
for(ll t=m;t>;t--){
if(t>=c[x])f[x][t]=f[x][t-c[x]]+v[x];
else f[x][t]=;
}
} int main(){
while(scanf("%lld%lld",&n,&m)!=EOF){
if(n==m&&m==-)break;
for(int i=;i<=n;i++){
scanf("%lld%lld",&c[i],&v[i]);
c[i]=(c[i]+)/;
} clear_(n);
for(int i=;i<n;i++){
ll u,v;
scanf("%lld%lld",&u,&v);
addedge(u,v);
}
dp(,);
printf("%lld\n",f[][m]);
}
return ;
}