#include <bits/stdc++.h>
using namespace std;
#define int long long
int const N=1e5+5;
int const V=103;
int dp[N][V];
int pei[N];
vector<int> adj[N];
void dfs(int node,int par=-1){
for(auto i:adj[node]){
if(i==par)
continue;
dfs(i,node);
}
for(int v=1;v<V;v++){
int c1=0;
int sm=0;
for(auto i:adj[node]){
if(i==par)
continue;
dp[node][v]=max(dp[node][v],dp[i][v]);
c1=max(c1,pei[i]);
}
c1*=-1;
for(auto i:adj[node]){
if(i==par)
continue;
sm+=pei[i];
c1=max(c1,dp[i][v-1]);
}
dp[node][v]=max(dp[node][v],sm+c1);
}
// cout<<node<<' '<<dp[node][0zzzzz]<<' '<<dp[node][1]<<' '<<dp[node][2]<<endl;
}
signed main(){
int n,v;
cin>>n>>v;
for(int i=1;i<=n;i++)
cin>>pei[i];
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
if(n<=1000){
int ans=0;
for(int i=1;i<=n;i++){
for(int node=1;node<=n;node++)
for(int vv=0;vv<V;vv++)
dp[node][vv]=0;
dfs(i);
ans=max(ans,dp[i][v]);
}
cout<<ans<<endl;
}
else{
dfs(1);
cout<<dp[1][v]<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4716 KB |
Output is correct |
7 |
Correct |
480 ms |
4696 KB |
Output is correct |
8 |
Correct |
507 ms |
4696 KB |
Output is correct |
9 |
Correct |
515 ms |
4948 KB |
Output is correct |
10 |
Correct |
485 ms |
4700 KB |
Output is correct |
11 |
Correct |
483 ms |
4752 KB |
Output is correct |
12 |
Correct |
488 ms |
4772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
90860 KB |
Output is correct |
2 |
Correct |
179 ms |
90960 KB |
Output is correct |
3 |
Correct |
245 ms |
88112 KB |
Output is correct |
4 |
Correct |
179 ms |
87912 KB |
Output is correct |
5 |
Correct |
183 ms |
87888 KB |
Output is correct |
6 |
Correct |
183 ms |
87892 KB |
Output is correct |
7 |
Correct |
181 ms |
87724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
2 ms |
4716 KB |
Output is correct |
7 |
Correct |
480 ms |
4696 KB |
Output is correct |
8 |
Correct |
507 ms |
4696 KB |
Output is correct |
9 |
Correct |
515 ms |
4948 KB |
Output is correct |
10 |
Correct |
485 ms |
4700 KB |
Output is correct |
11 |
Correct |
483 ms |
4752 KB |
Output is correct |
12 |
Correct |
488 ms |
4772 KB |
Output is correct |
13 |
Correct |
193 ms |
90860 KB |
Output is correct |
14 |
Correct |
179 ms |
90960 KB |
Output is correct |
15 |
Correct |
245 ms |
88112 KB |
Output is correct |
16 |
Correct |
179 ms |
87912 KB |
Output is correct |
17 |
Correct |
183 ms |
87888 KB |
Output is correct |
18 |
Correct |
183 ms |
87892 KB |
Output is correct |
19 |
Correct |
181 ms |
87724 KB |
Output is correct |
20 |
Incorrect |
187 ms |
89940 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |