#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, maxk = 1e2 + 10;
vector<int> grafo[maxn];
int n, k;
int64_t gain[maxn], x[maxn], dp[maxn][maxk][2], value[maxn][maxk], copia[maxn][maxk], gain_copy[maxn];
inline void add(int u, int v){
for(int i=1;i<=k;i++){
if(value[v][i] > dp[u][i][0]){
dp[u][i][1] = dp[u][i][0];
dp[u][i][0] = value[v][i];
}else if(value[v][i] > dp[u][i][1])
dp[u][i][1] = value[v][i];
}
gain[u] += x[v];
}
inline void del(int u, int v){
for(int i=1;i<=k;i++){
if(dp[u][i][0] == value[v][i]){
dp[u][i][0] = dp[u][i][1];
}
}
gain[u] -= x[v];
}
inline void make_copy(int u){
for(int i=0;i<=k;i++)
copia[u][i] = dp[u][i][0];
gain_copy[u] = gain[u];
}
inline void roll_back(int u){
for(int i=0;i<=k;i++)
dp[u][i][0] = copia[u][i];
gain[u] = gain_copy[u];
}
inline void compute(int u){
for(int i=1;i<=k;i++)
value[u][i] = max(dp[u][i][0], dp[u][i-1][0] + gain[u]);
}
void dfs(int u, int p=0){
for(auto v : grafo[u]){
if(v == p) continue;
dfs(v, u);
add(u, v);
}
compute(u);
}
int64_t ans;
void rotate(int u, int p=0){
compute(u);
make_copy(u);
ans = max(ans, value[u][k]);
for(auto v : grafo[u]){
if(v == p) continue;
del(u, v);
compute(u);
add(v, u);
rotate(v, u);
roll_back(u);
}
}
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);
rotate(1);
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2976 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2976 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2976 KB |
Output is correct |
7 |
Correct |
9 ms |
6432 KB |
Output is correct |
8 |
Correct |
9 ms |
6432 KB |
Output is correct |
9 |
Correct |
8 ms |
6456 KB |
Output is correct |
10 |
Correct |
11 ms |
6476 KB |
Output is correct |
11 |
Correct |
11 ms |
6476 KB |
Output is correct |
12 |
Correct |
11 ms |
6476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
746 ms |
357840 KB |
Output is correct |
2 |
Correct |
632 ms |
357840 KB |
Output is correct |
3 |
Correct |
534 ms |
357840 KB |
Output is correct |
4 |
Correct |
501 ms |
357840 KB |
Output is correct |
5 |
Correct |
771 ms |
357840 KB |
Output is correct |
6 |
Correct |
725 ms |
357840 KB |
Output is correct |
7 |
Correct |
712 ms |
357840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2808 KB |
Output is correct |
2 |
Correct |
5 ms |
2808 KB |
Output is correct |
3 |
Correct |
5 ms |
2976 KB |
Output is correct |
4 |
Correct |
5 ms |
2976 KB |
Output is correct |
5 |
Correct |
5 ms |
2976 KB |
Output is correct |
6 |
Correct |
4 ms |
2976 KB |
Output is correct |
7 |
Correct |
9 ms |
6432 KB |
Output is correct |
8 |
Correct |
9 ms |
6432 KB |
Output is correct |
9 |
Correct |
8 ms |
6456 KB |
Output is correct |
10 |
Correct |
11 ms |
6476 KB |
Output is correct |
11 |
Correct |
11 ms |
6476 KB |
Output is correct |
12 |
Correct |
11 ms |
6476 KB |
Output is correct |
13 |
Correct |
746 ms |
357840 KB |
Output is correct |
14 |
Correct |
632 ms |
357840 KB |
Output is correct |
15 |
Correct |
534 ms |
357840 KB |
Output is correct |
16 |
Correct |
501 ms |
357840 KB |
Output is correct |
17 |
Correct |
771 ms |
357840 KB |
Output is correct |
18 |
Correct |
725 ms |
357840 KB |
Output is correct |
19 |
Correct |
712 ms |
357840 KB |
Output is correct |
20 |
Correct |
741 ms |
357840 KB |
Output is correct |
21 |
Correct |
417 ms |
357840 KB |
Output is correct |
22 |
Correct |
813 ms |
357840 KB |
Output is correct |
23 |
Correct |
500 ms |
357840 KB |
Output is correct |
24 |
Correct |
775 ms |
357840 KB |
Output is correct |
25 |
Correct |
475 ms |
357840 KB |
Output is correct |
26 |
Correct |
726 ms |
357840 KB |
Output is correct |
27 |
Correct |
712 ms |
357840 KB |
Output is correct |