#include <bits/stdc++.h>
using namespace std;
long long dp[2][501][501];
long long n,m;
vector<int> adj[501];
int arr[501];
void dfs(int i,int pr){
for(int j = 1;j<=m;j++)dp[0][i][j] = arr[i],dp[1][i][j] = arr[i];
for(auto j:adj[i]){
if(j==pr)continue;
dfs(j,i);
for(int k = m;k>=1;k--){
for(int c = 1;c<k;c++){
dp[0][i][k] = max(dp[0][i][k],dp[0][j][c]+dp[1][i][k-(c+1)]);
if(k>c+1)dp[0][i][k] = max(dp[0][i][k],dp[1][j][c]+dp[0][i][k-(c+2)]);
if(k>c+1)dp[1][i][k] = max(dp[1][i][k],dp[1][j][c]+dp[1][i][k-(c+2)]);
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i = 1;i<=n;i++)cin>>arr[i];
for(int i = 0;i<n-1;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1,0);
cout<<max(dp[0][1][m],dp[1][1][m])<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1072 KB |
Output is correct |
2 |
Correct |
4 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1492 KB |
Output is correct |
2 |
Correct |
40 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
1852 KB |
Output is correct |
2 |
Correct |
22 ms |
1912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
2712 KB |
Output is correct |
2 |
Correct |
175 ms |
2692 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
3464 KB |
Output is correct |
2 |
Correct |
59 ms |
3384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
4292 KB |
Output is correct |
2 |
Correct |
31 ms |
4220 KB |
Output is correct |