#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
ll pigeon[mn], neighbour[mn], dpDown[2][2][mn], dpUp[2][2][mn], dpExcept[2][2][mn];
vector<int> adj[mn];
struct helper {
vector<ll> pre, sfx;
int n;
helper (vector<ll> a) : pre(a.size()), sfx(a.size()), n(a.size()) {
if (!n) return;
pre[0] = a[0], sfx[n - 1] = a[n - 1];
for (int i = 1; i < n; i++)
pre[i] = max(pre[i - 1], a[i]);
for (int i = n - 2; i >= 0; i--)
sfx[i] = max(sfx[i + 1], a[i]);
}
ll getPre (int p) {
return (0 <= p && p < n ? pre[p] : 0);
}
ll getSfx (int p) {
return (0 <= p && p < n ? sfx[p] : 0);
}
ll query (int exc = -1) {
if (exc == -1) return getPre(n - 1);
return max(getPre(exc - 1), getSfx(exc + 1));
}
};
void dfsDown (int u, int p, int bread) {
int t = bread & 1;
vector<ll> v1(adj[u].size()), v2(adj[u].size());
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i]; if (v == p) continue;
dfsDown(v, u, bread);
v1[i] = max(dpDown[t ^ 1][1][v] - pigeon[u], dpDown[t ^ 1][0][v]);
v2[i] = max(dpDown[t][1][v] - pigeon[u], dpDown[t][0][v]);
}
helper take(v1), ignore(v2);
dpDown[t][1][u] = take.query() + neighbour[u];
dpDown[t][0][u] = ignore.query();
for (int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i]; if (v == p) continue;
dpExcept[t][1][v] = take.query(i) + neighbour[u];
dpExcept[t][0][v] = ignore.query(i);
}
}
void dfsUp (int u, int p, int bread) {
int t = bread & 1;
dpUp[t][1][u] = neighbour[u];
if (u != p) {
ll take = max({dpUp[t ^ 1][1][p] - pigeon[u], dpUp[t ^ 1][0][p], dpExcept[t ^ 1][1][u] - pigeon[u], dpExcept[t ^ 1][0][u]});
ll ignore = max({dpUp[t][1][p] - pigeon[u], dpUp[t][0][p], dpExcept[t][1][u] - pigeon[u], dpExcept[t][0][u]});
dpUp[t][1][u] = take + neighbour[u], dpUp[t][0][u] = ignore;
}
for (int v : adj[u])
if (v != p) dfsUp(v, u, bread);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, v; cin >> n >> v;
for (int i = 1; i <= n; i++) cin >> pigeon[i];
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
neighbour[a] += pigeon[b];
neighbour[b] += pigeon[a];
adj[a].push_back(b);
adj[b].push_back(a);
}
if (v == 0) return cout << 0, 0;
for (int i = 1; i <= v; i++) {
dfsDown(1, 1, i);
dfsUp(1, 1, i);
}
ll ans = 0;
for (int i = 1; i <= n; i++)
ans = max({ans, dpDown[v & 1][1][i], dpUp[v & 1][1][i]});
cout << ans;
return 0;
}
Compilation message
chase.cpp: In function 'void dfsDown(int, int, int)':
chase.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < adj[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~
chase.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i = 0; i < adj[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
17 ms |
3500 KB |
Output is correct |
8 |
Correct |
3 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
21 ms |
3060 KB |
Output is correct |
11 |
Correct |
6 ms |
3160 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2605 ms |
38880 KB |
Output is correct |
2 |
Correct |
2468 ms |
38740 KB |
Output is correct |
3 |
Correct |
1416 ms |
25016 KB |
Output is correct |
4 |
Correct |
62 ms |
14416 KB |
Output is correct |
5 |
Correct |
2506 ms |
19004 KB |
Output is correct |
6 |
Correct |
2627 ms |
19028 KB |
Output is correct |
7 |
Correct |
2643 ms |
19024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
17 ms |
3500 KB |
Output is correct |
8 |
Correct |
3 ms |
3164 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
21 ms |
3060 KB |
Output is correct |
11 |
Correct |
6 ms |
3160 KB |
Output is correct |
12 |
Correct |
3 ms |
2892 KB |
Output is correct |
13 |
Correct |
2605 ms |
38880 KB |
Output is correct |
14 |
Correct |
2468 ms |
38740 KB |
Output is correct |
15 |
Correct |
1416 ms |
25016 KB |
Output is correct |
16 |
Correct |
62 ms |
14416 KB |
Output is correct |
17 |
Correct |
2506 ms |
19004 KB |
Output is correct |
18 |
Correct |
2627 ms |
19028 KB |
Output is correct |
19 |
Correct |
2643 ms |
19024 KB |
Output is correct |
20 |
Correct |
2657 ms |
19092 KB |
Output is correct |
21 |
Correct |
26 ms |
9556 KB |
Output is correct |
22 |
Correct |
2662 ms |
19024 KB |
Output is correct |
23 |
Correct |
65 ms |
14420 KB |
Output is correct |
24 |
Correct |
2512 ms |
19092 KB |
Output is correct |
25 |
Correct |
1448 ms |
24496 KB |
Output is correct |
26 |
Correct |
2731 ms |
38996 KB |
Output is correct |
27 |
Correct |
2600 ms |
39064 KB |
Output is correct |