/**
* author: wxhtzdy
* created: 21.12.2022 21:21:48
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<vector<vector<int>>> dp(n, vector<vector<int>>(m + 1, vector<int>(2)));
function<void(int, int)> Solve = [&](int v, int pv) {
for (int u : g[v]) {
if (u == pv) {
continue;
}
Solve(u, v);
for (int i = m - 1; i >= 0; i--) {
for (int j = 0; j <= m; j++) {
int new_i = j + i + 2;
if (new_i <= m) {
dp[v][new_i][1] = max(dp[v][new_i][1], dp[v][i][1] + dp[u][j][0]);
dp[v][new_i][0] = max(dp[v][new_i][0], dp[v][i][0] + dp[u][j][0]);
dp[v][new_i - 1][1] = max(dp[v][new_i - 1][1], dp[v][i][0] + dp[u][j][1]);
}
}
}
}
for (int i = m - 1; i >= 0; i--) {
dp[v][i + 1][0] = max(dp[v][i + 1][0], dp[v][i][0] + a[v]);
dp[v][i + 1][1] = max(dp[v][i + 1][1], dp[v][i][1] + a[v]);
}
};
Solve(0, 0);
int ans = 0;
for (int i = 0; i <= m; i++) {
ans = max(ans, dp[0][i][1]);
ans = max(ans, dp[0][i][0]);
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
1344 KB |
Output is correct |
2 |
Correct |
6 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
1004 KB |
Output is correct |
2 |
Correct |
98 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
180 ms |
4780 KB |
Output is correct |
2 |
Correct |
54 ms |
2776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
3484 KB |
Output is correct |
2 |
Correct |
396 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
356 ms |
9156 KB |
Output is correct |
2 |
Correct |
145 ms |
5884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
678 ms |
14108 KB |
Output is correct |
2 |
Correct |
61 ms |
4528 KB |
Output is correct |