#include<bits/stdc++.h>
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define pp pair<ll, ll>
#define inf 1000000000000000
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
vi val;
struct edge{
ll from;
ll to;
vi dp;
bool ready = false;
};
vector<vector<edge>> edges;
void readyEdge(edge &e){
ll sum = 0;
for(int i = 0; i < edges[e.to].size(); i++){
ll v = edges[e.to][i].to;
if(v != e.from){
sum += val[v];
if(!edges[e.to][i].ready){
readyEdge(edges[e.to][i]);
}
}
}
for(int i = 1; i < e.dp.size(); i++){
ll MAX1 = 0, MAX2 = 0;
for(int j = 0; j < edges[e.to].size(); j++){
ll v = edges[e.to][j].to;
if(v != e.from){
MAX1 = max(MAX1, edges[e.to][j].dp[i-1]);
MAX2 = max(MAX2, edges[e.to][j].dp[i]);
}
}
e.dp[i] = max(MAX1 + sum, MAX2);
}
e.ready = true;
}
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio(false);
ll n, v;
cin >> n >> v;
val.resize(n, 0);
rep(i,n){
cin >> val[i];
}
edges.resize(n);
rep(i, n-1){
ll a, b;
cin >> a >> b;
a--; b--;
edge e, e1;
e.from = a;
e.to = b;
e.dp.resize(v + 1, 0);
e1.from = b;
e1.to = a;
e1.dp.resize(v + 1, 0);
edges[a].push_back(e);
edges[b].push_back(e1);
}
if(v == 0){
cout << 0 << endl;
return 0;
}
rep(i, n){
rep(j, edges[i].size()){
if(!edges[i][j].ready){
readyEdge(edges[i][j]);
}
}
if(n > 1000){
break;
}
}
ll ans = 0;
rep(i, n){
ll sum = 0;
ll MAXV = 0, MAXV1 = 0;
rep(j, edges[i].size()){
sum += val[edges[i][j].to];
MAXV = max(MAXV, edges[i][j].dp[v]);
MAXV1 = max(MAXV1, edges[i][j].dp[v - 1]);
}
ans = max(ans, max(sum + MAXV1, MAXV));
if(n > 1000){
break;
}
}
cout << ans << endl;
}
Compilation message
chase.cpp: In function 'void readyEdge(edge&)':
chase.cpp:26:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int i = 0; i < edges[e.to].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~
chase.cpp:35:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i = 1; i < e.dp.size(); i++){
| ~~^~~~~~~~~~~~~
chase.cpp:37:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int j = 0; j < edges[e.to].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:8:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define rep(i,n) for(int i = 0; i < n; i++)
......
79 | rep(j, edges[i].size()){
| ~~~~~~~~~~~~~~~~~~
chase.cpp:79:9: note: in expansion of macro 'rep'
79 | rep(j, edges[i].size()){
| ^~~
chase.cpp:8:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | #define rep(i,n) for(int i = 0; i < n; i++)
......
92 | rep(j, edges[i].size()){
| ~~~~~~~~~~~~~~~~~~
chase.cpp:92:9: note: in expansion of macro 'rep'
92 | rep(j, edges[i].size()){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
2136 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2140 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
180928 KB |
Output is correct |
2 |
Correct |
305 ms |
180944 KB |
Output is correct |
3 |
Correct |
298 ms |
174072 KB |
Output is correct |
4 |
Correct |
132 ms |
22132 KB |
Output is correct |
5 |
Correct |
294 ms |
175440 KB |
Output is correct |
6 |
Correct |
289 ms |
175320 KB |
Output is correct |
7 |
Correct |
280 ms |
175552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
4 ms |
2136 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
10 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2140 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
277 ms |
180928 KB |
Output is correct |
14 |
Correct |
305 ms |
180944 KB |
Output is correct |
15 |
Correct |
298 ms |
174072 KB |
Output is correct |
16 |
Correct |
132 ms |
22132 KB |
Output is correct |
17 |
Correct |
294 ms |
175440 KB |
Output is correct |
18 |
Correct |
289 ms |
175320 KB |
Output is correct |
19 |
Correct |
280 ms |
175552 KB |
Output is correct |
20 |
Incorrect |
262 ms |
177492 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |