#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 1e5 + 5;
int n, k;
int a[N];
long long s[N], f[N][101], g[N][101], dp[101];
vector<int> tr[N];
long long dfs(int u, int p) {
long long res = 0;
for (auto v : tr[u]) {
if (v ^ p) {
res = max(res, dfs(v, u));
}
}
for (int i = 1; i <= k; ++i) {
f[u][i] = dp[i] = s[u];
}
for (int v : tr[u]) {
for (int i = 1; i <= k; ++i) {
dp[i] = max({dp[i], dp[i - 1], f[v][i], f[v][i - 1] + s[u] - a[v]});
g[u][i] = max({g[u][i], g[u][i - 1], g[v][i], g[v][i - 1] + s[u] - a[p]});
}
for (int i = 0; i <= k; ++i) {
res = max(res, f[u][i] + g[v][k - i]);
f[u][i] = dp[i];
}
}
for (int i = 1; i <= k; ++i) {
f[u][i] = dp[i] = s[u];
}
reverse(tr[u].begin(), tr[u].end());
for (int v : tr[u]) {
for (int i = 1; i <= k; ++i) {
dp[i] = max({dp[i], dp[i - 1], f[v][i], f[v][i - 1] + s[u] - a[v]});
}
for (int i = 0; i <= k; ++i) {
res = max(res, f[u][i] + g[v][k - i]);
f[u][i] = dp[i];
}
}
return res;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> k;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
tr[u].push_back(v);
tr[v].push_back(u);
s[u] += a[v];
s[v] += a[u];
}
cout << dfs(1, 1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
4 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6824 KB |
Output is correct |
12 |
Correct |
2 ms |
6828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
323 ms |
174160 KB |
Output is correct |
2 |
Correct |
335 ms |
174160 KB |
Output is correct |
3 |
Correct |
300 ms |
167628 KB |
Output is correct |
4 |
Correct |
114 ms |
167252 KB |
Output is correct |
5 |
Correct |
359 ms |
167216 KB |
Output is correct |
6 |
Correct |
351 ms |
167248 KB |
Output is correct |
7 |
Correct |
349 ms |
167420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6744 KB |
Output is correct |
2 |
Correct |
1 ms |
6748 KB |
Output is correct |
3 |
Correct |
1 ms |
6748 KB |
Output is correct |
4 |
Correct |
1 ms |
6744 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
6748 KB |
Output is correct |
7 |
Correct |
4 ms |
6748 KB |
Output is correct |
8 |
Correct |
1 ms |
6748 KB |
Output is correct |
9 |
Correct |
2 ms |
6748 KB |
Output is correct |
10 |
Correct |
3 ms |
6748 KB |
Output is correct |
11 |
Correct |
2 ms |
6824 KB |
Output is correct |
12 |
Correct |
2 ms |
6828 KB |
Output is correct |
13 |
Correct |
323 ms |
174160 KB |
Output is correct |
14 |
Correct |
335 ms |
174160 KB |
Output is correct |
15 |
Correct |
300 ms |
167628 KB |
Output is correct |
16 |
Correct |
114 ms |
167252 KB |
Output is correct |
17 |
Correct |
359 ms |
167216 KB |
Output is correct |
18 |
Correct |
351 ms |
167248 KB |
Output is correct |
19 |
Correct |
349 ms |
167420 KB |
Output is correct |
20 |
Correct |
342 ms |
167252 KB |
Output is correct |
21 |
Correct |
76 ms |
90192 KB |
Output is correct |
22 |
Correct |
343 ms |
167252 KB |
Output is correct |
23 |
Correct |
109 ms |
167252 KB |
Output is correct |
24 |
Correct |
360 ms |
167332 KB |
Output is correct |
25 |
Correct |
312 ms |
167304 KB |
Output is correct |
26 |
Correct |
337 ms |
174416 KB |
Output is correct |
27 |
Correct |
336 ms |
174416 KB |
Output is correct |