#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3yqVz8E ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
signed main(){
_3yqVz8E;
int n,m;
cin>>n>>m;
vi a(n);
rep(i,n){
cin>>a[i];
}
vec(vi) adj(n);
rep(i,n-1){
int u,v;
cin>>u>>v;
u-=1,v-=1;
adj[u].pb(v);
adj[v].pb(u);
}
vi par(n,-1);
auto dfs=[&](auto self,int v,int _par)->void{
par[v]=_par;
for(auto u:adj[v]){
if(u!=_par){
self(self,u,v);
}
}
};
dfs(dfs,0,-1);
vec(vi) leb(n,vi(n,-1));
rep(v,n){
adj[v].pb(v);
int si=sz(adj[v]);
rep(j,si){
leb[v][adj[v][j]]=j;
}
}
const int inf=1e9;
vec(vec(vi)) dp;
auto affine=[&](int i,int v,int u,int val)->void{
if(val<0) return;
int j=leb[v][u];
dp[i][v][j]=max(dp[i][v][j],val);
};
// don't take root
int ans=0;
rep(type,2){
if(type){
m-=1;
}
dp=vec(vec(vi))(m+1,vec(vi)(n));
rep(i,m+1){
rep(v,n){
int si=sz(adj[v]);
rep(j,si){
dp[i][v].pb(-inf);
}
}
}
if(!type){
affine(0,0,0,0);
}else{
affine(0,0,0,a[0]);
}
rep(i,m){
rep(v,n){
int _par=par[v];
int now=dp[i][v][leb[v][v]];
if(_par!=-1){
affine(i+1,v,v,dp[i][v][leb[v][_par]]+a[v]);
now=max(now,dp[i][v][leb[v][_par]]);
}
int si=sz(adj[v]);
rep(j,si){
int u=adj[v][j];
if(u==_par or u==v) continue;
affine(i+1,u,v,now);
now=max(now,dp[i][v][j]);
}
if(_par!=-1){
affine(i+1,_par,v,now);
}
}
}
rep(i,m+1){
rep(v,n){
int si=sz(adj[v]);
rep(j,si){
ans=max(ans,dp[i][v][j]);
}
}
}
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
724 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
1748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
1364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
6996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
32 ms |
5268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
13896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
118 ms |
21488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |