#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pb push_back
#define endl "\n"
#define all(x) x.begin(), x.end()
const int M = 100007;
const ll inf = 1e9;
const ll mod = 998244353;
const double pi = acos(-1);
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
vector <int> adj[M];
pair <ll,ll> mx[M][105][2];
int n, v, a[M], who[M][105][2];
ll dp[M][105], ans;
void dfs(int node, int p)
{
ll sum = 0;
for(auto i : adj[node]) if(i != p) sum += a[i];
for(auto i : adj[node]) if(i != p){
dfs(i, node);
for(int j = 1; j <= v; ++j) dp[node][j] = max(dp[node][j], max(dp[i][j], dp[i][j - 1] + sum));
}
return;
}
void reroot(int node, int p)
{
ll sum = 0;
for(auto i : adj[node]) sum += a[i];
for(auto i : adj[node]) for(int j = 1; j <= v; ++j) dp[node][j] = max(dp[node][j], max(dp[i][j], dp[i][j - 1] + sum));
ans = max(ans, dp[node][v]);
for(auto i : adj[node]){
for(int j = 1; j <= v; ++j){
for(int k = 0; k < 2; ++k){
if(dp[i][j - k] > mx[node][j][k].F){
mx[node][j][k].S = mx[node][j][k].F;
mx[node][j][k].F = dp[i][j - k];
who[node][j][k] = i;
}
else if(dp[i][j - k] > mx[node][j][k].S) mx[node][j][k].S = dp[i][j - k];
}
}
}
for(auto i : adj[node]) if(i != p){
for(int j = 1; j <= v; ++j){
ll x[2] = {0};
for(int k = 0; k < 2; ++k){
if(who[node][j][k] == i) x[k] = mx[node][j][k].S;
else x[k] = mx[node][j][k].F;
}
dp[node][j] = max(x[0], x[1] + sum - a[i]);
}
reroot(i, node);
}
return;
}
int main()
{
scanf("%d%d", &n, &v);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i < n; ++i){
int x, y;
scanf("%d%d", &x, &y);
adj[x].pb(y);
adj[y].pb(x);
}
dfs(1, 0);
reroot(1, 0);
printf("%lld\n", ans);
return 0;
}
Compilation message
chase.cpp: In function 'int main()':
chase.cpp:68:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d%d", &n, &v);
| ~~~~~^~~~~~~~~~~~~~~~
chase.cpp:69:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
chase.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
6 ms |
7628 KB |
Output is correct |
8 |
Correct |
5 ms |
7628 KB |
Output is correct |
9 |
Correct |
5 ms |
7524 KB |
Output is correct |
10 |
Correct |
6 ms |
7628 KB |
Output is correct |
11 |
Correct |
5 ms |
7628 KB |
Output is correct |
12 |
Correct |
6 ms |
7628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
489 ms |
506820 KB |
Output is correct |
2 |
Correct |
484 ms |
506784 KB |
Output is correct |
3 |
Correct |
376 ms |
501732 KB |
Output is correct |
4 |
Correct |
313 ms |
501468 KB |
Output is correct |
5 |
Correct |
502 ms |
501464 KB |
Output is correct |
6 |
Correct |
500 ms |
501440 KB |
Output is correct |
7 |
Correct |
529 ms |
501516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2648 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
6 ms |
7628 KB |
Output is correct |
8 |
Correct |
5 ms |
7628 KB |
Output is correct |
9 |
Correct |
5 ms |
7524 KB |
Output is correct |
10 |
Correct |
6 ms |
7628 KB |
Output is correct |
11 |
Correct |
5 ms |
7628 KB |
Output is correct |
12 |
Correct |
6 ms |
7628 KB |
Output is correct |
13 |
Correct |
489 ms |
506820 KB |
Output is correct |
14 |
Correct |
484 ms |
506784 KB |
Output is correct |
15 |
Correct |
376 ms |
501732 KB |
Output is correct |
16 |
Correct |
313 ms |
501468 KB |
Output is correct |
17 |
Correct |
502 ms |
501464 KB |
Output is correct |
18 |
Correct |
500 ms |
501440 KB |
Output is correct |
19 |
Correct |
529 ms |
501516 KB |
Output is correct |
20 |
Correct |
502 ms |
501424 KB |
Output is correct |
21 |
Correct |
62 ms |
8520 KB |
Output is correct |
22 |
Correct |
495 ms |
501460 KB |
Output is correct |
23 |
Correct |
362 ms |
501428 KB |
Output is correct |
24 |
Correct |
520 ms |
501480 KB |
Output is correct |
25 |
Correct |
379 ms |
501468 KB |
Output is correct |
26 |
Correct |
457 ms |
506840 KB |
Output is correct |
27 |
Correct |
459 ms |
506864 KB |
Output is correct |