#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];
ll n, v, a[M], who[M][105][2], 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][i] = 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("%lld%lld", &n, &v);
for(int i = 1; i <= n; ++i) scanf("%lld", &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:67:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%lld%lld", &n, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
chase.cpp:68:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
| ~~~~~^~~~~~~~~~~~~~~
chase.cpp:71:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
419 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2636 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |