#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(),a.end()
#define pii pair <int,int>
#define Pii pair< pii , pii >
#define int long long
using namespace std ;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10 , maxq = 1e7 + 1 , inf = 1e18 + 10 , mod= 998244353 ,lg = 20 ;
int sm[maxn] , dp[maxn][102][2] , w[maxn] , ans = 0, n , k;
vector <int> G[maxn] ;
int mx[102] ;
void dfs(int v, int p =0){
sm[v] += w[p] ;
for(int i = 0 ; i < G[v].size() ; i++){
int u = G[v][i] ;
if(u == p)continue ;
sm[v] += w[u];
dfs(u , v) ;
}
for(int i =0 ; i < G[v].size() ; i++){
int u= G[v][i] ;
if(u==p)continue ;
for(int i =0 ; i <= k ; i++){
if(i!=0)ans = max(ans , dp[u][i-1][0] + sm[v]) ;
dp[v][i][0] = max({dp[v][i][0] , dp[u][i][0] , (i!=0 ?dp[u][i-1][0] + sm[v] - w[p] : -inf)});
}
for(int i =0 ; i <= k ; i++){
if(i!=0)dp[v][i][1] = max(dp[v][i][1] , sm[v]);
dp[v][i][1] = max({dp[v][i][1] , dp[u][i][1] , (i!=0 ?dp[u][i-1][1] + sm[v] - w[u]: -inf)});
}
}
ans = max(ans , dp[v][k][1]) ;
ans = max(ans , sm[v]) ;
for(int i = 0 ; i <= k ; i++)mx[i] =0 ;
mx[0] = -1 ;
for(int i = 0 ; i < G[v].size() ; i++){
int u = G[v][i] ;
if(u == p)continue ;
if(mx[0]!=-1){
for(int i = 1 ; i <k ; i++){
ans = max(ans , mx[k-i] + dp[u][i][1]) ;
}
for(int i = 2; i < k ; i++){
ans = max(ans,mx[k-i] + dp[u][i-1][1] + sm[v] - w[u]) ;
}
}
for(int i =0 ; i <= k ; i++){
mx[i] = max(mx[i] , dp[u][i][0]) ;
}
}
reverse(all(G[v]));
for(int i = 0 ; i <= k ; i++)mx[i] =0 ;
mx[0] = -1 ;
for(int i = 0 ; i < G[v].size() ; i++){
int u = G[v][i] ;
if(u == p)continue ;
if(mx[0]!=-1){
for(int i = 1 ; i <k ; i++){
ans = max(ans , mx[k-i] + dp[u][i][1]) ;
}
for(int i = 2; i < k ; i++){
ans = max(ans,mx[k-i] + dp[u][i-1][1] + sm[v] - w[u]) ;
}
}
for(int i =0 ; i <= k ; i++){
mx[i] = max(mx[i] , dp[u][i][0]) ;
}
}
}
signed main(){
ios::sync_with_stdio(false); cin.tie(0) ;
cin >> n >> k ;
for(int i = 1; i <= n ; i++){
cin >> w[i] ;
}
for(int i = 1; i < n; i++){
int v, u ;
cin >> v>> u ;
G[v].pb(u);G[u].pb(v) ;
}
if(k ==0 ){
cout << 0 ;
return 0 ;
}
dfs(1 , 0) ;
cout << ans ;
}
Compilation message
chase.cpp: In function 'void dfs(long long int, long long int)':
chase.cpp:20:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i = 0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:26:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i =0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:42:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for(int i = 0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:60:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int i = 0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
2 ms |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Incorrect |
2 ms |
4696 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
95056 KB |
Output is correct |
2 |
Correct |
178 ms |
95168 KB |
Output is correct |
3 |
Correct |
159 ms |
9668 KB |
Output is correct |
4 |
Correct |
100 ms |
121168 KB |
Output is correct |
5 |
Correct |
215 ms |
131920 KB |
Output is correct |
6 |
Correct |
220 ms |
131636 KB |
Output is correct |
7 |
Correct |
205 ms |
131412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4696 KB |
Output is correct |
4 |
Correct |
1 ms |
4696 KB |
Output is correct |
5 |
Correct |
2 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4696 KB |
Output is correct |
7 |
Correct |
2 ms |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Incorrect |
2 ms |
4696 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |