#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]);
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], e[x][i - 1][0]);
e[x][i][1] = max(e[x][i][1], e[x][i - 1][1]);
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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |