This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |