#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int K = 105;
long long dp[N][K][2][2];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vector<int>> g(n);
for (int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<long long> s(n);
for (int i = 0; i < n; i++) {
for (int j : g[i]) {
s[i] += a[j];
}
}
long long res = 0;
function<void(int, int)> Solve = [&](int v, int pv) {
dp[v][1][0][1] = s[v];
dp[v][1][1][1] = s[v];
for (int i = 1; i <= k; i++) {
for (int d = 0; d < 2; d++) {
for (int t = 0; t < 2; t++) {
dp[v][i][d][t] = max(dp[v][i][d][t], dp[v][i - 1][d][t]);
}
}
}
for (int u : g[v]) {
if (u == pv) {
continue;
}
Solve(u, v);
for (int i = 0; i <= k; i++) {
for (int me = 0; me < 2; me++) {
for (int he = 0; he < 2; he++) {
{
long long ft = (he == 0 ? 0 : -a[v]);
res = max(res, dp[v][i][0][me] + dp[u][k - i][1][he] + ft);
}
{
long long ft = (me == 0 ? 0 : -a[u]);
res = max(res, dp[v][i][1][me] + dp[u][k - i][0][he] + ft);
}
}
}
}
for (int i = 0; i <= k; i++) {
for (int me = 0; me < 2; me++) {
for (int he = 0; he < 2; he++) {
long long ft = (me == 0 ? 0 : s[v] - a[u]);
dp[v][i + me][0][me] = max(dp[v][i + me][0][me], dp[u][i][0][he] + ft);
}
}
}
for (int i = 0; i <= k; i++) {
for (int me = 0; me < 2; me++) {
for (int he = 0; he < 2; he++) {
long long ft = (me == 0 ? 0 : s[v]);
if (he == 1) {
ft -= a[v];
}
dp[v][i + me][1][me] = max(dp[v][i + me][1][me], dp[u][i][1][he] + ft);
}
}
}
for (int i = 1; i <= k; i++) {
for (int d = 0; d < 2; d++) {
for (int t = 0; t < 2; t++) {
dp[v][i][d][t] = max(dp[v][i][d][t], dp[v][i - 1][d][t]);
}
}
}
}
};
Solve(0, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
for (int d = 0; d < 2; d++) {
for (int t = 0; t < 2; t++) {
res = max(res, dp[i][j][d][t]);
}
}
}
}
cout << res << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 KB |
Output is correct |
9 |
Correct |
2 ms |
3676 KB |
Output is correct |
10 |
Correct |
4 ms |
3672 KB |
Output is correct |
11 |
Correct |
2 ms |
3676 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
403 ms |
344304 KB |
Output is correct |
2 |
Correct |
414 ms |
344476 KB |
Output is correct |
3 |
Correct |
413 ms |
336236 KB |
Output is correct |
4 |
Correct |
135 ms |
335952 KB |
Output is correct |
5 |
Correct |
441 ms |
335832 KB |
Output is correct |
6 |
Correct |
447 ms |
335868 KB |
Output is correct |
7 |
Correct |
481 ms |
335972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
5 ms |
3676 KB |
Output is correct |
8 |
Correct |
2 ms |
3676 KB |
Output is correct |
9 |
Correct |
2 ms |
3676 KB |
Output is correct |
10 |
Correct |
4 ms |
3672 KB |
Output is correct |
11 |
Correct |
2 ms |
3676 KB |
Output is correct |
12 |
Correct |
2 ms |
3676 KB |
Output is correct |
13 |
Correct |
403 ms |
344304 KB |
Output is correct |
14 |
Correct |
414 ms |
344476 KB |
Output is correct |
15 |
Correct |
413 ms |
336236 KB |
Output is correct |
16 |
Correct |
135 ms |
335952 KB |
Output is correct |
17 |
Correct |
441 ms |
335832 KB |
Output is correct |
18 |
Correct |
447 ms |
335868 KB |
Output is correct |
19 |
Correct |
481 ms |
335972 KB |
Output is correct |
20 |
Correct |
414 ms |
335820 KB |
Output is correct |
21 |
Correct |
153 ms |
335952 KB |
Output is correct |
22 |
Correct |
445 ms |
335768 KB |
Output is correct |
23 |
Correct |
154 ms |
335952 KB |
Output is correct |
24 |
Correct |
434 ms |
335956 KB |
Output is correct |
25 |
Correct |
385 ms |
336076 KB |
Output is correct |
26 |
Correct |
419 ms |
344400 KB |
Output is correct |
27 |
Correct |
433 ms |
344404 KB |
Output is correct |