#include <bits/stdc++.h>
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 1e5 + 10;
int n, m;
long long in[N][110];
long long out[N][110];
int c[N];
long long tot[N];
vector <int> g[N];
long long prf[N][110], suf[N][110];
int child[N];
void down(int u, int p) {
for (auto v: g[u]) {
if (v == p) continue;
down(v, u);
for (int i = 1; i <= m; ++i)
in[u][i] = max({in[u][i], in[v][i], in[v][i - 1] + tot[v] - c[u]});
}
}
void up(int u, int p) {
int num = 0;
for (auto v: g[u]) {
if (v == p) continue;
child[++num] = v;
}
for (int i = 0; i <= m; ++i) prf[0][i] = suf[num + 1][i];
for (int i = 1; i <= num; ++i) {
int v = child[i];
for (int j = 1; j <= m; ++j) {
prf[i][j] = max({prf[i - 1][j], in[v][j], in[v][j - 1] + tot[v] - c[u]});
}
}
for (int i = num; i >= 1; --i) {
int v = child[i];
for (int j = 1; j <= m; ++j) {
suf[i][j] = max({suf[i + 1][j], in[v][j], in[v][j - 1] + tot[v] - c[u]});
}
}
for (int i = 1; i <= num; ++i) {
int v = child[i];
for (int j = 1; j <= m; ++j) {
out[v][j] = max(out[u][j], out[u][j - 1] + tot[u] - c[v]);
out[v][j] = max({out[v][j], prf[i - 1][j], suf[i + 1][j]});
out[v][j] = max({out[v][j], prf[i - 1][j - 1] + tot[u] - c[v], suf[i + 1][j - 1] + tot[u] - c[v]});
}
}
for (auto v: g[u]) {
if (v == p) continue;
up(v, u);
}
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> c[i];
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int u = 1; u <= n; ++u)
for (auto v: g[u])
tot[u] += c[v];
down(1, 0);
up(1, 0);
long long ans = 0;
for (int u = 1; u <= n; ++u) {
ans = max({ans, in[u][m], out[u][m]});
for (int i = 0; i < m; ++i) {
ans = max({ans, in[u][i] + tot[u], out[u][i] + tot[u]});
}
}
cout << ans;
}
Compilation message
chase.cpp:2: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
2 | #pragma GCC optimization ("O3")
|
chase.cpp:3: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
3 | #pragma GCC optimization ("unroll-loops")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
4 ms |
4044 KB |
Output is correct |
8 |
Correct |
3 ms |
4100 KB |
Output is correct |
9 |
Correct |
5 ms |
5240 KB |
Output is correct |
10 |
Correct |
4 ms |
4300 KB |
Output is correct |
11 |
Correct |
4 ms |
4356 KB |
Output is correct |
12 |
Correct |
4 ms |
4300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
143852 KB |
Output is correct |
2 |
Correct |
238 ms |
143804 KB |
Output is correct |
3 |
Correct |
253 ms |
268384 KB |
Output is correct |
4 |
Correct |
150 ms |
165964 KB |
Output is correct |
5 |
Correct |
315 ms |
168300 KB |
Output is correct |
6 |
Correct |
301 ms |
168424 KB |
Output is correct |
7 |
Correct |
301 ms |
168264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2680 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2676 KB |
Output is correct |
6 |
Correct |
1 ms |
2636 KB |
Output is correct |
7 |
Correct |
4 ms |
4044 KB |
Output is correct |
8 |
Correct |
3 ms |
4100 KB |
Output is correct |
9 |
Correct |
5 ms |
5240 KB |
Output is correct |
10 |
Correct |
4 ms |
4300 KB |
Output is correct |
11 |
Correct |
4 ms |
4356 KB |
Output is correct |
12 |
Correct |
4 ms |
4300 KB |
Output is correct |
13 |
Correct |
245 ms |
143852 KB |
Output is correct |
14 |
Correct |
238 ms |
143804 KB |
Output is correct |
15 |
Correct |
253 ms |
268384 KB |
Output is correct |
16 |
Correct |
150 ms |
165964 KB |
Output is correct |
17 |
Correct |
315 ms |
168300 KB |
Output is correct |
18 |
Correct |
301 ms |
168424 KB |
Output is correct |
19 |
Correct |
301 ms |
168264 KB |
Output is correct |
20 |
Correct |
311 ms |
168236 KB |
Output is correct |
21 |
Correct |
80 ms |
9484 KB |
Output is correct |
22 |
Correct |
317 ms |
168132 KB |
Output is correct |
23 |
Correct |
160 ms |
166084 KB |
Output is correct |
24 |
Correct |
301 ms |
168332 KB |
Output is correct |
25 |
Correct |
257 ms |
267896 KB |
Output is correct |
26 |
Correct |
243 ms |
143876 KB |
Output is correct |
27 |
Correct |
237 ms |
143836 KB |
Output is correct |