#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 10, maxk = 1e2 + 10;
vector<int> grafo[maxn];
int n, k;
int gain[maxn], x[maxn], dp[10][maxn][maxk], dp2[10][maxn][maxk], ans[maxn];
void dfs(int u, int p=0){
gain[u] = 0;
for(auto v : grafo[u]){
if(v == p) continue;
gain[u] += x[v];
dfs(v, u);
}
}
inline void calcDp(int u, int p=0){
for(int l=1;l<=k;l++)
for(int x=0;x<2;x++) dp[x][u][l] = dp2[x][u][l] = 0;
for(auto v : grafo[u]){
if(v == p) continue;
for(int l=1;l<=k;l++){
int x = max(dp[0][v][l], dp[1][v][l]);
int y = max(dp[0][v][l-1], dp[1][v][l-1]) + gain[u];
if(dp[0][u][l] < y) dp2[0][u][l] = dp[0][u][l], dp[0][u][l] = y;
else if(dp2[0][u][l] < y) dp2[0][u][l] = y;
if(dp[1][u][l] < x) dp2[1][u][l] = dp[1][u][l], dp[1][u][l] = x;
else if(dp2[1][u][l] < x) dp2[1][u][l] = x;
}
}
}
void solve(int u, int p=0){
for(auto v : grafo[u]){
if(v == p) continue;
solve(v, u);
}
calcDp(u, p);
}
void rotate(int u, int p=0){
ans[u] = max(dp[0][u][k], dp[1][u][k]);
// cout << "ans(" << u << "): " << ans[u] << " " << max(dp2[0][u][k], dp2[1][u][k]) << "\n";
for(auto v : grafo[u]){
if(v == p) continue;
int x1 = gain[u], x2 = gain[v];
int x3[k+1], x4[k+1], x5[k+1], x6[k+1], x7[k+1], x8[k+1];
for(int l=1;l<=k;l++)
x3[l] = dp[0][u][l], x4[l] = dp[1][u][l], x5[l] = dp[0][v][l], x6[l] = dp[1][v][l], x7[l] = dp2[0][v][l], x8[l] = dp2[1][v][l];
for(int l=1;l<=k;l++){
int a = max(dp[0][v][l], dp[1][v][l]);
int b = max(dp[0][v][l-1], dp[1][v][l-1]) + gain[u];
if(dp[1][u][l] == a) dp[1][u][l] = dp2[1][u][l];
if(dp[0][u][l] == b) dp[0][u][l] = dp2[0][u][l];
if(dp[0][u][l] != 0) dp[0][u][l] -= x[v];
}
gain[u] -= x[v];
gain[v] += x[u];
calcDp(v);
rotate(v, u);
gain[u] = x1, gain[v] = x2;
for(int l=1;l<=k;l++)
dp[0][u][l] = x3[l], dp[1][u][l] = x4[l], dp[0][v][l] = x5[l], dp[1][v][l] = x6[l], dp2[0][v][l] = x7[l], dp2[1][v][l] = x8[l];
}
}
int32_t main(){
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> k;
for(int i=1;i<=n;i++)
cin >> x[i];
for(int i=1;i<n;i++){
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
dfs(1);
solve(1);
//rotate(1);
int resp=0;
for(int i=1;i<=n;i++){
// cout << "gain: " << gain[i] << "\n";
// cout << "dp: " << dp[i][k] << "\n";
resp = max(resp, ans[i]);
}
// int resp2=0;
// for(int i=1;i<=n;i++){
// dfs(i);
// solve(i);
// resp2 = max({resp2, dp[0][i][k], dp[1][i][k]});
// cout << "ans2(" << i << "): " << max(dp[0][i][k], dp[1][i][k]) << " " << max(dp2[0][i][k], dp2[1][i][k]) << "\n";
// }
cout << resp << "\n";
// cout << resp2 << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
858 ms |
358432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |