#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#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 = 2e5 + 10 , maxq = 1e7 + 1 , inf = 1e18 + 10 , mod= 998244353 ,lg = 20 ;
int dis[maxn] , k , n , w[maxn] , dp[maxn][102][3] , sm[maxn];
vector <int> G[maxn] ;
int ans= 0 ;
void dfs(int v, int p =0 ){
sm[v] = 0 ;
for(int i =0 ; i < G[v].size() ; i++){
int u =G[v][i] ;
if(u == p)continue ;
sm[v] += w[u] ;
}
for(int i = 1 ; i <= k ; i++){
dp[v][i][0]= max(dp[p][i][0] , dp[p][i][1]) ;
dp[v][i][1]= dp[p][i][2] ;
dp[v][i][2] = max({dp[p][i-1][0] + sm[v] , dp[p][i-1][1] + sm[v] , dp[p][i-1][2] + sm[v]});
ans= max({ans , dp[v][i][1] , dp[v][i][0] , dp[v][i][2]}) ;
}
for(int i =0 ; i < G[v].size() ; i++){
int u =G[v][i] ;
if(u == p)continue ;
dfs(u, v);
}
}
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 = 0 ; i <= k ; i++){
dp[0][i][1] = -inf ;
dp[0][i][0] = -inf ;
dp[0][i][2] = -inf ;
}
dp[0][0][0] = dp[0][0][1] = dp[0][0][2] = 0 ;
for(int i = 1; i < n; i++){
int v, u ;
cin >> v>> u ;
G[v].pb(u);G[u].pb(v) ;
}
if(n > 1000){
dfs(1, 0) ;
cout << ans ;
return 0 ;
}
for(int i = 1; i <= n ; i++){
dfs(i , 0);
}
cout << ans ;
}
/*
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: In function 'void dfs(long long int, long long int)':
chase.cpp:22: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]
22 | for(int i =0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
chase.cpp:33: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]
33 | for(int i =0 ; i < G[v].size() ; i++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8792 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8792 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
319 ms |
13024 KB |
Output is correct |
8 |
Correct |
28 ms |
12888 KB |
Output is correct |
9 |
Correct |
24 ms |
13008 KB |
Output is correct |
10 |
Correct |
313 ms |
12992 KB |
Output is correct |
11 |
Correct |
112 ms |
12992 KB |
Output is correct |
12 |
Correct |
40 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
257872 KB |
Output is correct |
2 |
Correct |
118 ms |
260180 KB |
Output is correct |
3 |
Correct |
79 ms |
254844 KB |
Output is correct |
4 |
Correct |
64 ms |
254712 KB |
Output is correct |
5 |
Correct |
129 ms |
254748 KB |
Output is correct |
6 |
Correct |
130 ms |
254752 KB |
Output is correct |
7 |
Correct |
124 ms |
254752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
2 ms |
8792 KB |
Output is correct |
5 |
Correct |
2 ms |
8792 KB |
Output is correct |
6 |
Correct |
2 ms |
8792 KB |
Output is correct |
7 |
Correct |
319 ms |
13024 KB |
Output is correct |
8 |
Correct |
28 ms |
12888 KB |
Output is correct |
9 |
Correct |
24 ms |
13008 KB |
Output is correct |
10 |
Correct |
313 ms |
12992 KB |
Output is correct |
11 |
Correct |
112 ms |
12992 KB |
Output is correct |
12 |
Correct |
40 ms |
12892 KB |
Output is correct |
13 |
Correct |
120 ms |
257872 KB |
Output is correct |
14 |
Correct |
118 ms |
260180 KB |
Output is correct |
15 |
Correct |
79 ms |
254844 KB |
Output is correct |
16 |
Correct |
64 ms |
254712 KB |
Output is correct |
17 |
Correct |
129 ms |
254748 KB |
Output is correct |
18 |
Correct |
130 ms |
254752 KB |
Output is correct |
19 |
Correct |
124 ms |
254752 KB |
Output is correct |
20 |
Incorrect |
125 ms |
254660 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |