#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10, maxk = 1e2 + 10;
vector<int> grafo[maxn];
int n, k;
ll gain[maxn], x[maxn], dp[2][maxn][maxk], dp2[2][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++){
ll x = max(dp[0][v][l], dp[1][v][l]);
ll 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(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;
solve(v, u);
for(int l=1;l<=k;l++){
ll x = max(dp[0][v][l], dp[1][v][l]);
ll 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;
}
}
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;
ll x1 = gain[u], x2 = gain[v];
ll 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++){
ll a = max(dp[0][v][l], dp[1][v][l]);
ll 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];
}
}
int 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);
ll 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 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2924 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
6 ms |
2976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2924 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
6 ms |
2976 KB |
Output is correct |
7 |
Correct |
24 ms |
9020 KB |
Output is correct |
8 |
Correct |
9 ms |
9020 KB |
Output is correct |
9 |
Correct |
12 ms |
9020 KB |
Output is correct |
10 |
Correct |
16 ms |
9020 KB |
Output is correct |
11 |
Correct |
11 ms |
9020 KB |
Output is correct |
12 |
Correct |
10 ms |
9020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2564 ms |
525312 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2808 KB |
Output is correct |
2 |
Correct |
4 ms |
2924 KB |
Output is correct |
3 |
Correct |
4 ms |
2924 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
6 ms |
2976 KB |
Output is correct |
7 |
Correct |
24 ms |
9020 KB |
Output is correct |
8 |
Correct |
9 ms |
9020 KB |
Output is correct |
9 |
Correct |
12 ms |
9020 KB |
Output is correct |
10 |
Correct |
16 ms |
9020 KB |
Output is correct |
11 |
Correct |
11 ms |
9020 KB |
Output is correct |
12 |
Correct |
10 ms |
9020 KB |
Output is correct |
13 |
Runtime error |
2564 ms |
525312 KB |
Memory limit exceeded: We have a known bug that the memory usage is measured incorrectly (possibly because of Meltdown/Spectre patch), so your solution may be correct. Please submit again. Sorry for the inconvenience. |
14 |
Halted |
0 ms |
0 KB |
- |