#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 = 200007;
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));
for(int j = 0; j <= v; ++j) 0ans = max(ans, dp[node][j]);
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){
ll x[2];
for(int j = 1; j <= v; ++j){
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 a, b;
scanf("%d%d", &a, &b);
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1, 0);
reroot(1, 0);
printf("%lld\n", ans);
return 0;
}
Compilation message
chase.cpp: In function 'void reroot(int, int)':
chase.cpp:36:30: error: unable to find numeric literal operator 'operator""ans'
36 | for(int j = 0; j <= v; ++j) 0ans = max(ans, dp[node][j]);
| ^~~~
chase.cpp:36:30: note: use '-fext-numeric-literals' to enable more built-in suffixes
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", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~