//曇り空 のぞいた予感
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll , ll> pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10;
const ll mod = 1e9+7;
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n , m , sz[maxn];
ll p[maxn];
bool hide[maxn];
vector < int > adj[maxn];
ll ans = 0;
ll dp[maxn][110] , pd[maxn][110];
void plant(int v){
hide[v] = 1;
sz[v] = 1;
for(auto u : adj[v])if(!hide[u])
plant(u) , sz[v] += sz[u];
hide[v] = 0;
}
int cen(int v , int n , int par = 0 , bool found = 0){
while(!found){
found = 1;
for(auto u : adj[v])if(!hide[u] and u ^ par and sz[u] * 2 > n){
found = 0 , par = v , v = u;
break;
}
}
return v;
}
ll out[maxn][110] , in[maxn][110];
ll sum[maxn];
void dfs(int v , int par){
sum[v] = 0;
for(int i = 1 ; i <= m ; i ++)pd[v][i] = dp[v][i] = 0;
for(auto u : adj[v])if(u^par)sum[v] += p[u];
for(auto u : adj[v])if(!hide[u] and u ^ par){
dfs(u , v);
for(int i = 1 ; i <= m ; i ++){
dp[v][i] = max(dp[v][i] , dp[u][i]);
dp[v][i] = max(dp[v][i] , dp[u][i-1] + sum[u] - p[v]);
pd[v][i] = max(pd[v][i] , pd[u][i]);
pd[v][i] = max(pd[v][i] , pd[u][i-1] + sum[v] - p[u]);
}
}
}
void calc(int v){
sum[v] = 0;
for(int i = 1 ; i <= m ; i ++)pd[v][i] = dp[v][i] = 0;
for(auto u : adj[v])sum[v] += p[u];
for(auto u : adj[v])if(!hide[u]){
dfs(u , v);
for(int i = 1 ; i <= m ; i ++){
ans = max(ans , pd[v][i] + dp[v][m-i]);
pd[v][i] = max(pd[v][i] , pd[u][i]);
pd[v][i] = max(pd[v][i] , pd[u][i-1] + sum[v] - p[u]);
dp[v][i] = max(dp[v][i] , dp[u][i]);
dp[v][i] = max(dp[v][i] , dp[u][i-1] + sum[u] - p[v]);
ans = max(ans , dp[v][i]);
ans = max(ans , pd[v][i]);
}
}
}
void solve(int v = 1){
plant(v);
v = cen(v , sz[v]);
calc(v);
hide[v] = 1;
for(auto u : adj[v])if(!hide[u])
solve(u);
}
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++)
cin >> p[i];
for(int i = 1 ; i < n ; i ++){
int u , v;
cin >> u >> v;
sum[v] += p[u];sum[u] += p[v];
adj[u].pb(v);
adj[v].pb(u);
}
solve();
cout << ans;
return(0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1260 ms |
183288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |