#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] ;
vector <pii> vec[102] , vec2[102] , vec3[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 <= k ; i++){
vec[i].clear();vec2[i].clear();vec3[i].clear();
}
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++){
vec[i].pb({dp[u][i][0] , u}) ;
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++){
vec2[i].pb({dp[u][i][1] , u}) ;
vec3[i].pb({dp[u][i][1]-w[u]+sm[v] , u}) ;
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)});
}
}
for(int i =0 ; i <= k ; i++){
ans = max({ans , dp[v][i][1]});
sort(all(vec[i]));reverse(all(vec[i]));
sort(all(vec2[i]));reverse(all(vec2[i]));
sort(all(vec3[i]));reverse(all(vec3[i]));
}
for(int i =0 ; i < G[v].size() ; i++){
int u = G[v][i] ;
if(u == p)continue ;
for(int j = 0 ; j <= k ; j++){
for(int a = 0 ; a < min(2ll , (int)vec[j].size()) ; a++){
for(int b =0 ; b < min(2ll , (int)vec2[k-j].size()) ; b++){
if(vec[j][a].S != vec2[k-j][b].S){
ans = max(ans , vec[j][a].F + vec2[k-j][b].F) ;
}
}
}
if(j == k || k==j+1 )continue ;
for(int a = 0 ; a < min(2ll , (int)vec[j].size()) ; a++){
for(int b =0 ; b < min(2ll , (int)vec3[k-j-1].size()) ; b++){
if(vec[j][a].S != vec3[k-j-1][b].S){
ans = max(ans , vec[j][a].F + vec3[k-j-1][b].F) ;
}
}
}
}
}
}
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) ;
}
dfs(1 , 0) ;
cout << ans ;
}
Compilation message
chase.cpp: In function 'void dfs(long long int, long long int)':
chase.cpp:21: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]
21 | for(int i = 0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:30: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]
30 | for(int i =0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:51: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]
51 | 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 |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4520 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 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 |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4520 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
7 ms |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
624 ms |
96748 KB |
Output is correct |
2 |
Correct |
626 ms |
96520 KB |
Output is correct |
3 |
Runtime error |
704 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
1 ms |
4520 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
7 ms |
4696 KB |
Output is correct |
8 |
Correct |
2 ms |
4700 KB |
Output is correct |
9 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |