#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], u, v, s[NMAX][105], e[NMAX][105][2], ans;
vector<int> adj[NMAX];
void dfs(int x, int bef){
ll sum = 0;
for(int nx : adj[x]) sum += a[nx];
s[x][1] = e[x][1][1] = sum;
ans = max(ans, sum);
for(int nx : adj[x]){
if(nx == bef) continue;
dfs(nx, x);
for(int i = 1; i <= k; i++) {
ans = max(ans, s[x][i] + max(e[nx][k - i][0], e[nx][k - i][1] - a[x]));
ans = max(ans, max(e[x][i][0] + s[nx][k - i], e[x][i][1] + s[nx][k - i] - a[nx]));
s[x][i] = max(s[x][i], s[x][i - 1]);
e[x][i][0] = max(e[x][i][0], e[x][i - 1][0]);
e[x][i][1] = max(e[x][i][1], e[x][i - 1][1]);
s[x][i] = max({s[x][i], s[nx][i], s[nx][i - 1] + sum - a[nx]});
e[x][i][0] = max(e[x][i][0], max(e[nx][i][0], e[nx][i][1] - a[x]));
e[x][i][1] = max(e[x][i][1], max(e[nx][i - 1][0] + sum, e[nx][i - 1][1] + sum - a[x]));
}
}
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 >> u >> v;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
dfs(1, 0);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
234 ms |
259324 KB |
Output is correct |
2 |
Correct |
249 ms |
259316 KB |
Output is correct |
3 |
Correct |
233 ms |
253540 KB |
Output is correct |
4 |
Correct |
146 ms |
253168 KB |
Output is correct |
5 |
Correct |
258 ms |
253252 KB |
Output is correct |
6 |
Correct |
266 ms |
253252 KB |
Output is correct |
7 |
Correct |
259 ms |
253264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
1 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |