#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
int n,m;
int dp[501][501];
int dp2[501][501];
int it[501];
vector<int> adj[501];
void dfs(int no,int par2=-1){
for(auto j:adj[no]){
if(j==par2){
continue;
}
dfs(j,no);
}
for(auto j:adj[no]){
if(j==par2){
continue;
}
for(int k=m;k>=1;k--){
for(int i=min(k-1,m);i>=1;i--){
dp2[no][k]=max(dp2[no][k],dp[no][k-i-1]+dp2[j][i]);
}
for(int i=min(k-2,m);i>=1;i--){
dp2[no][k]=max(dp2[no][k],dp2[no][k-i-2]+dp[j][i]);
dp[no][k]=max(dp[no][k],dp[no][k-i-2]+dp[j][i]);
}
}
}
for(int i=m;i>=1;i--){
dp[no][i]=max(dp[no][i],dp[no][i-1]+it[no]);
dp2[no][i]=max(dp2[no][i],dp2[no][i-1]+it[no]);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>it[i];
}
for(int i=0;i<n-1;i++){
int aa,bb;
cin>>aa>>bb;
aa--;
bb--;
adj[aa].pb(bb);
adj[bb].pb(aa);
}
dfs(0);
cout<<max(dp[0][m],dp2[0][m])<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
768 KB |
Output is correct |
2 |
Correct |
3 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
896 KB |
Output is correct |
2 |
Correct |
27 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
1152 KB |
Output is correct |
2 |
Correct |
16 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
1536 KB |
Output is correct |
2 |
Correct |
130 ms |
1572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
97 ms |
1912 KB |
Output is correct |
2 |
Correct |
40 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
192 ms |
2348 KB |
Output is correct |
2 |
Correct |
20 ms |
2304 KB |
Output is correct |