This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n, V;
int val[100005];
bool viz[100005];
long long Sol, sum[100005];
long long down[100005][105][2], up[100005][105][2];
long long up2[100005][105][2], down2[100005][105][2];
long long up3[100005][105][2];
vector <int> v[100005];
void dfs(int nod = 1){
viz[nod] = 1;
down[nod][1][1] = max(down[nod][1][1], sum[nod]);
up[nod][1][1] = max(up[nod][1][1], sum[nod]);
for(auto fiu : v[nod]){
if(viz[fiu]) continue ;
dfs(fiu);
for(int k = 0; k <= V ; ++k){
Sol = max(Sol, down[nod][k][0] + up[fiu][V - k][0]);
Sol = max(Sol, down[nod][k][0] + up[fiu][V - k][1]);
Sol = max(Sol, down[nod][k][1] + up[fiu][V - k][0] - val[fiu]);
Sol = max(Sol, down[nod][k][1] + up[fiu][V - k][1] - val[fiu]);
Sol = max(Sol, down[fiu][k][0] + up[nod][V - k][0]);
Sol = max(Sol, down[fiu][k][0] + up[nod][V - k][1]);
Sol = max(Sol, down[fiu][k][1] + up[nod][V - k][0] - val[nod]);
Sol = max(Sol, down[fiu][k][1] + up[nod][V - k][1] - val[nod]);
}
for(int k = 1; k <= V ; ++k){
down[nod][k][0] = max(max(down[nod][k - 1][0], down[nod][k][0]), max(down[fiu][k][0], down[fiu][k][1] - val[nod]));
down[nod][k][1] = max(max(down[nod][k - 1][1], down[nod][k][1]), max(down[fiu][k - 1][0], down[fiu][k - 1][1] - val[nod]) + sum[nod]);
up[nod][k][0] = max(max(up[nod][k][0], up[nod][k - 1][0]), max(up[fiu][k][0], up[fiu][k][1]));
up[nod][k][1] = max(max(up[nod][k][1], up[nod][k - 1][1]), max(up[fiu][k - 1][0] - val[fiu], up[fiu][k - 1][1] - val[fiu]) + sum[nod]);
Sol = max(Sol, max(up[nod][k][0], up[nod][k][1]));
Sol = max(Sol, max(down[nod][k][0], down[nod][k][1]));
}
}
}
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d%d", &n, &V);
for(int i = 1; i <= n ; ++i)
scanf("%d", &val[i]);
int x, y;
for(int i = 2; i <= n ; ++i){
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
sum[x] += val[y];
sum[y] += val[x];
}
dfs();
printf("%lld", Sol);
return 0;
}
Compilation message (stderr)
chase.cpp: In function 'int main()':
chase.cpp:54:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &V);
~~~~~^~~~~~~~~~~~~~~~
chase.cpp:56:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &val[i]);
~~~~~^~~~~~~~~~~~~~~
chase.cpp:60:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |