#include<bits/stdc++.h>
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 100010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout<<"\n";
#define int long long
#define inf (int)1e18
#define pii pair<int,int>
#define vii vector<pii>
#define lb(x) x&-x
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(1<<j))
#define vi vector<int>
template <typename T1, typename T2> bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
int n,V;
vi ke[maxn];
int p[maxn],s[maxn];
int up[maxn][110],down[maxn][110];
int ans;
void dfs(int u,int parent)
{
up[u][1] = s[u];
for(int v : ke[u])
{
if(v == parent) continue;
dfs(v,u);
fo(i,1,V) ans = max(ans,up[u][i] + down[v][V - i]);
fo(i,1,V)
{
up[u][i] = max({up[u][i],up[v][i],up[v][i - 1] + s[u] - p[v]});
down[u][i] = max({down[u][i],down[v][i],down[u][i - 1] + s[u] - p[parent]});
}
}
reverse(ke[u].begin(),ke[u].end());
fo(i,1,V) up[u][i] = 0;
up[u][1] = s[u];
for(int v : ke[u])
{
if(v == parent) continue;
fo(i,1,V) ans = max(ans,up[u][i] + down[v][V - i]);
fo(i,1,V)
{
up[u][i] = max({up[u][i],up[v][i],up[v][i - 1] + s[u] - p[v]});
down[u][i] = max({down[u][i],down[v][i],down[v][i - 1] + s[u] - p[parent]});
}
}
}
main()
{
#define name "TASK"
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> V;
fo(i,1,n) cin >> p[i];
fo(i,1,n - 1)
{
int u,v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
s[v] += p[u];
s[u] += p[v];
}
dfs(1,0);
cout << ans;
}
Compilation message
chase.cpp:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main()
| ^~~~
chase.cpp: In function 'int main()':
chase.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
chase.cpp:77:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
149 ms |
144464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
8024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |