#include<bits/stdc++.h>
using namespace std;
const int N = 505;
int n, m, a[N], dp[N][N][2];
vector < int > g[N];
void dfs(int v, int pr = -1){
dp[v][0][0] = dp[v][0][1] = 0;
dp[v][1][1] = dp[v][1][0] = a[v];
for(int to : g[v]){
if(to != pr){
dfs(to, v);
for(int i = m; i >= 1; i--){
for(int j = 0; j < i; j++){
if(i - j - 1 >= 0){
dp[v][i][0] = max(dp[v][i][0], dp[v][j][1] + dp[to][i - j - 1][0]);
}
if(i - j - 2 >= 0){
dp[v][i][1] = max(dp[v][i][1], dp[v][j][1] + dp[to][i - j - 2][1]);
dp[v][i][0] = max(dp[v][i][0], dp[v][j][0] + dp[to][i - j - 2][1]);
}
}
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
memset(dp, -63, sizeof(dp));
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i < n; i++){
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs(1);
int ans = 0;
for(int i = 1; i <= m; i++){
ans = max({ans, dp[1][i][0], dp[1][i][1]});
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2432 KB |
Output is correct |
2 |
Correct |
7 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2304 KB |
Output is correct |
2 |
Correct |
37 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
2400 KB |
Output is correct |
2 |
Correct |
31 ms |
2400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
2304 KB |
Output is correct |
2 |
Correct |
199 ms |
2432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
2304 KB |
Output is correct |
2 |
Correct |
58 ms |
2408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
266 ms |
2432 KB |
Output is correct |
2 |
Correct |
24 ms |
2304 KB |
Output is correct |