#include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,i,j,ii,jj,zx,xc,dp[503][503][2],f[503],dp2[503][503][2];
vector <int> v[503];
void dfs(int q, int w){
for(vector <int>::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it)==w) continue;
dfs((*it),q);
}
dp[q][1][0]=f[q];dp[q][1][1]=f[q];
for(vector <int>::iterator it=v[q].begin(); it!=v[q].end(); it++){
if((*it)==w) continue;
for(j=0; j<=b; j++){
dp2[q][j][0]=dp[q][j][0];
dp2[q][j][1]=dp[q][j][1];
}
for(j=0; j<=b; j++){
for(jj=0; jj<=b; jj++){
c=j+jj+2;
if(c<=b){
if(dp2[q][c][0]<dp[q][j][0]+dp[(*it)][jj][0]){
dp2[q][c][0]=dp[q][j][0]+dp[(*it)][jj][0];
}
}
c=j+jj+1;
if(c<=b){
if(dp2[q][c][1]<dp[q][j][0]+dp[(*it)][jj][1]){
dp2[q][c][1]=dp[q][j][0]+dp[(*it)][jj][1];
}
}
c=j+jj+2;
if(c<=b){
if(dp2[q][c][1]<dp[q][j][1]+dp[(*it)][jj][0]){
dp2[q][c][1]=dp[q][j][1]+dp[(*it)][jj][0];
}
}
}
}
for(j=0; j<=b; j++){
dp[q][j][0]=dp2[q][j][0];
dp[q][j][1]=max(dp2[q][j][0],dp2[q][j][1]);
}
}
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
for(i=1; i<=a; i++) cin>>f[i];
for(i=1; i<a; i++){
cin>>c>>d;
v[c].push_back(d);
v[d].push_back(c);
}
dfs(1,0);
c=0;
// cout<<dp[3][1][0]<<endl;
for(i=0; i<=b; i++){
//cout<<dp[1][i][0]<<" "<<dp[1][i][1]<<endl;
if(c<dp[1][i][1]) c=dp[1][i][1];
}
cout<<c;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
1004 KB |
Output is correct |
2 |
Correct |
5 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1260 KB |
Output is correct |
2 |
Correct |
54 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
1772 KB |
Output is correct |
2 |
Correct |
32 ms |
1692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
2284 KB |
Output is correct |
2 |
Correct |
225 ms |
1772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
197 ms |
3052 KB |
Output is correct |
2 |
Correct |
84 ms |
3180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
375 ms |
3948 KB |
Output is correct |
2 |
Correct |
47 ms |
2540 KB |
Output is correct |