#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=1e5+5;
vector <int> g[N];
int fr_sum[N],x[N];
int dp[N][105];
int n,k;
void dfs(int v,int p){
for(auto to : g[v]){
if(to!=p){
for(int i=1;i<=k;i++)dp[to][i]=max(dp[v][i],dp[v][i-1]+fr_sum[to]-x[v]);
dfs(to,v);
}
}
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>x[i];
for(int i=1;i<n;i++){
int u,v;
cin>>u>>v;
g[u].pb(v);
g[v].pb(u);
fr_sum[v]+=x[u];
fr_sum[u]+=x[v];
}
int ans=0;
if(n<=1000){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int t=0;t<=k;t++){
dp[j][t]=0;
}
}
for(int j=1;j<=k;j++){
dp[i][j]=fr_sum[i];
}
dfs(i,0);
for(int j=1;j<=n;j++){
for(int t=0;t<=k;t++){
ans=max(ans,dp[j][t]);
}
}
}
cout<<ans<<"\n";
return 0;
}
for(int j=1;j<=k;j++){
dp[1][j]=fr_sum[1];
}
dfs(1,0);
for(int j=1;j<=n;j++){
for(int t=0;t<=k;t++){
ans=max(ans,dp[j][t]);
}
}
cout<<ans<<"\n";
}
/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
232 ms |
8612 KB |
Output is correct |
8 |
Correct |
23 ms |
8616 KB |
Output is correct |
9 |
Correct |
20 ms |
8540 KB |
Output is correct |
10 |
Correct |
249 ms |
8580 KB |
Output is correct |
11 |
Correct |
84 ms |
8536 KB |
Output is correct |
12 |
Correct |
35 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
136 ms |
94040 KB |
Output is correct |
2 |
Correct |
131 ms |
94036 KB |
Output is correct |
3 |
Correct |
107 ms |
90324 KB |
Output is correct |
4 |
Correct |
122 ms |
90332 KB |
Output is correct |
5 |
Correct |
173 ms |
90376 KB |
Output is correct |
6 |
Correct |
147 ms |
90376 KB |
Output is correct |
7 |
Correct |
149 ms |
90152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6236 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6236 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
232 ms |
8612 KB |
Output is correct |
8 |
Correct |
23 ms |
8616 KB |
Output is correct |
9 |
Correct |
20 ms |
8540 KB |
Output is correct |
10 |
Correct |
249 ms |
8580 KB |
Output is correct |
11 |
Correct |
84 ms |
8536 KB |
Output is correct |
12 |
Correct |
35 ms |
8540 KB |
Output is correct |
13 |
Correct |
136 ms |
94040 KB |
Output is correct |
14 |
Correct |
131 ms |
94036 KB |
Output is correct |
15 |
Correct |
107 ms |
90324 KB |
Output is correct |
16 |
Correct |
122 ms |
90332 KB |
Output is correct |
17 |
Correct |
173 ms |
90376 KB |
Output is correct |
18 |
Correct |
147 ms |
90376 KB |
Output is correct |
19 |
Correct |
149 ms |
90152 KB |
Output is correct |
20 |
Incorrect |
193 ms |
90152 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |