#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 1e5 + 5;
ll n, k, a[NMAX], x, y, u[NMAX][105], d[NMAX][105], ans;
vector<int> adj[NMAX];
void dfs(int x, int bef){
ll sum = 0;
for(int nx : adj[x]) sum += a[nx];
u[x][1] = sum;
for(int nx : adj[x]){
if(nx == bef) continue;
dfs(nx, x);
for(int i = 1; i <= k; i++){
ans = max(ans, u[x][i] + d[nx][k - i]);
u[x][i] = max({u[x][i], u[x][i - 1], u[nx][i], u[nx][i - 1] + sum - a[nx]});
d[x][i] = max({d[x][i], d[x][i - 1], d[nx][i], d[nx][i - 1] + sum - a[bef]});
}
}
reverse(all(adj[x]));
memset(u[x], 0, sizeof(u[x]));
memset(d[x], 0, sizeof(d[x]));
u[x][1] = sum;
for(int nx : adj[x]){
if(nx == bef) continue;
for(int i = 1; i <= k; i++){
ans = max(ans, u[x][i] + d[nx][k - i]);
u[x][i] = max({u[x][i], u[x][i - 1], u[nx][i], u[nx][i - 1] + sum - a[nx]});
d[x][i] = max({d[x][i], d[x][i - 1], d[nx][i], d[nx][i - 1] + sum - a[bef]});
}
}
return;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 0; i < n - 1; i++){
cin >> x >> y;
adj[x].emplace_back(y);
adj[y].emplace_back(x);
}
dfs(1, 0);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2680 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2680 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
4 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4352 KB |
Output is correct |
9 |
Correct |
3 ms |
4308 KB |
Output is correct |
10 |
Correct |
4 ms |
4356 KB |
Output is correct |
11 |
Correct |
3 ms |
4372 KB |
Output is correct |
12 |
Correct |
3 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
264 ms |
180888 KB |
Output is correct |
2 |
Correct |
273 ms |
180812 KB |
Output is correct |
3 |
Correct |
221 ms |
173408 KB |
Output is correct |
4 |
Correct |
156 ms |
173156 KB |
Output is correct |
5 |
Correct |
280 ms |
173152 KB |
Output is correct |
6 |
Correct |
264 ms |
173148 KB |
Output is correct |
7 |
Correct |
287 ms |
173164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2680 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
4 ms |
4436 KB |
Output is correct |
8 |
Correct |
4 ms |
4352 KB |
Output is correct |
9 |
Correct |
3 ms |
4308 KB |
Output is correct |
10 |
Correct |
4 ms |
4356 KB |
Output is correct |
11 |
Correct |
3 ms |
4372 KB |
Output is correct |
12 |
Correct |
3 ms |
4352 KB |
Output is correct |
13 |
Correct |
264 ms |
180888 KB |
Output is correct |
14 |
Correct |
273 ms |
180812 KB |
Output is correct |
15 |
Correct |
221 ms |
173408 KB |
Output is correct |
16 |
Correct |
156 ms |
173156 KB |
Output is correct |
17 |
Correct |
280 ms |
173152 KB |
Output is correct |
18 |
Correct |
264 ms |
173148 KB |
Output is correct |
19 |
Correct |
287 ms |
173164 KB |
Output is correct |
20 |
Correct |
268 ms |
173108 KB |
Output is correct |
21 |
Correct |
158 ms |
173096 KB |
Output is correct |
22 |
Correct |
306 ms |
173156 KB |
Output is correct |
23 |
Correct |
165 ms |
173136 KB |
Output is correct |
24 |
Correct |
265 ms |
173088 KB |
Output is correct |
25 |
Correct |
226 ms |
173108 KB |
Output is correct |
26 |
Correct |
263 ms |
180948 KB |
Output is correct |
27 |
Correct |
293 ms |
180880 KB |
Output is correct |