#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define ll long long
#define int long long
const int N=1e5+5;
int p[N];
vector<int> g[N];
int c[N][105],b[N][105];
int G(int x){
//int res=-=p[par];
int res=0;//<---------------
for(auto it: g[x]){
res+=p[it];
}
return res;
}
int n,k;
int ans=0;
void dfs(int x, int par){
int sum=G(x);
b[x][1]=sum;
for(auto it: g[x]){
if(it==par) continue;
dfs(it,x);
for(int j=1;j<=k;j++){
ans=max(ans,b[x][j]+c[it][k-j]);
c[x][j]=max(c[x][j],max(c[it][j],c[it][j-1]+sum-p[par]));
b[x][j]=max(b[x][j],max(b[it][j],b[it][j-1]+sum-p[it]));
}
}
reverse(all(g[x]));
memset(c[x],0,sizeof c[x]);
memset(b[x],0,sizeof b[x]);
b[x][1]=sum;
for(auto it: g[x]){
if(it==par) continue;
for(int j=1;j<=k;j++){
ans=max(ans,b[x][j]+c[it][k-j]);
c[x][j]=max(c[x][j],max(c[it][j],c[it][j-1]+sum-p[par]));
b[x][j]=max(b[x][j],max(b[it][j],b[it][j-1]+sum-p[it]));
}
}
}
void solve(){
//int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>p[i];
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
dfs(1,0);
cout<<ans<<endl;
}
main(){
int T=1;
//cin>>T;
while(T--){
solve();
}
}
/*
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
*/
Compilation message
chase.cpp:66:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
66 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
4444 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
3 ms |
4392 KB |
Output is correct |
11 |
Correct |
3 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
179796 KB |
Output is correct |
2 |
Correct |
225 ms |
179724 KB |
Output is correct |
3 |
Correct |
241 ms |
174056 KB |
Output is correct |
4 |
Correct |
203 ms |
173752 KB |
Output is correct |
5 |
Correct |
234 ms |
173756 KB |
Output is correct |
6 |
Correct |
247 ms |
173752 KB |
Output is correct |
7 |
Correct |
237 ms |
173648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
2 ms |
2648 KB |
Output is correct |
6 |
Correct |
2 ms |
2652 KB |
Output is correct |
7 |
Correct |
4 ms |
4444 KB |
Output is correct |
8 |
Correct |
4 ms |
4444 KB |
Output is correct |
9 |
Correct |
3 ms |
4440 KB |
Output is correct |
10 |
Correct |
3 ms |
4392 KB |
Output is correct |
11 |
Correct |
3 ms |
4444 KB |
Output is correct |
12 |
Correct |
3 ms |
4444 KB |
Output is correct |
13 |
Correct |
244 ms |
179796 KB |
Output is correct |
14 |
Correct |
225 ms |
179724 KB |
Output is correct |
15 |
Correct |
241 ms |
174056 KB |
Output is correct |
16 |
Correct |
203 ms |
173752 KB |
Output is correct |
17 |
Correct |
234 ms |
173756 KB |
Output is correct |
18 |
Correct |
247 ms |
173752 KB |
Output is correct |
19 |
Correct |
237 ms |
173648 KB |
Output is correct |
20 |
Correct |
233 ms |
173808 KB |
Output is correct |
21 |
Correct |
165 ms |
173652 KB |
Output is correct |
22 |
Correct |
227 ms |
173648 KB |
Output is correct |
23 |
Correct |
167 ms |
173652 KB |
Output is correct |
24 |
Correct |
226 ms |
173772 KB |
Output is correct |
25 |
Correct |
178 ms |
173512 KB |
Output is correct |
26 |
Correct |
228 ms |
179864 KB |
Output is correct |
27 |
Correct |
228 ms |
180052 KB |
Output is correct |