#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9 + 1e6 + 123;
const ll LINF = (ll) 1e18 + 1e9 + 123;
#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
bool mini(auto &x, const auto &y) {
if (y < x) {
x = y;
return 1;
}
return 0;
}
bool maxi(auto &x, const auto &y) {
if (y > x) {
x = y;
return 1;
}
return 0;
}
void run();
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
run();
return 0;
}
const int N = (int) 1e5 + 123;
const int CNT = 101;
int n, cc;
vector<int> g[N];
ll p[N], around[N];
ll up[N][CNT], down[N][CNT][2];
ll ans;
ll best_up[CNT], best_strange[CNT];
void dfs(int v, int f = 0) {
maxi(up[v][1], around[v]);
maxi(down[v][1][1], around[v]);
int ptr = -1;
int id = 0;
for (int u : g[v]) {
if (u == f) {
ptr = id;
id++;
continue;
}
dfs(u, v);
id++;
}
if (ptr != -1) {
g[v].erase(g[v].begin() + ptr);
}
for (int u : g[v]) {
rep(i, 1, cc + 1) {
maxi(up[v][i], up[u][i]);
maxi(up[v][i], up[u][i - 1] + around[v] - p[u]);
maxi(down[v][i][0], max(down[u][i][0], down[u][i][1] - p[v]));
maxi(down[v][i][1], max(down[u][i - 1][0] + around[v], down[u][i - 1][1] - p[v] + around[v]));
}
}
rep(i, 1, cc + 1) {
maxi(up[v][i], up[v][i - 1]);
rep(j, 0, 2) {
maxi(down[v][i][j], down[v][i - 1][j]);
}
}
maxi(ans, up[v][cc]);
maxi(ans, max(down[v][cc][0], down[v][cc][1]));
rep(iter, 0, 2) {
rep(i, 0, cc + 1) {
best_up[i] = -LINF;
best_strange[i] = -LINF;
}
for (int u : g[v]) {
rep(i, 1, cc + 1) {
maxi(ans, down[u][i][0] + best_up[cc - i]);
maxi(ans, down[u][i][1] - p[v] + best_up[cc - i]);
maxi(ans, down[u][i - 1][0] + best_strange[cc - i] + around[v]);
maxi(ans, down[u][i - 1][1] - p[v] + best_strange[cc - i] + around[v]);
}
rep(i, 1, cc + 1) {
maxi(best_up[i], up[u][i]);
maxi(best_up[i], best_up[i - 1]);
maxi(best_strange[i], up[u][i] - p[u]);
maxi(best_strange[i], best_strange[i - 1]);
}
}
reverse(all(g[v]));
}
}
void run() {
cin >> n >> cc;
rep(i, 1, n + 1) {
cin >> p[i];
}
rep(i, 0, n - 1) {
int a, b;
cin >> a >> b;
g[a].pb(b);
g[b].pb(a);
}
rep(i, 1, n + 1) {
for (int j : g[i]) {
around[i] += p[j];
}
}
dfs(1);
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2792 KB |
Output is correct |
3 |
Correct |
12 ms |
2792 KB |
Output is correct |
4 |
Correct |
3 ms |
2792 KB |
Output is correct |
5 |
Correct |
3 ms |
2860 KB |
Output is correct |
6 |
Correct |
4 ms |
2860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2792 KB |
Output is correct |
3 |
Correct |
12 ms |
2792 KB |
Output is correct |
4 |
Correct |
3 ms |
2792 KB |
Output is correct |
5 |
Correct |
3 ms |
2860 KB |
Output is correct |
6 |
Correct |
4 ms |
2860 KB |
Output is correct |
7 |
Correct |
8 ms |
5196 KB |
Output is correct |
8 |
Correct |
8 ms |
5200 KB |
Output is correct |
9 |
Correct |
7 ms |
5324 KB |
Output is correct |
10 |
Correct |
9 ms |
5380 KB |
Output is correct |
11 |
Correct |
7 ms |
5404 KB |
Output is correct |
12 |
Correct |
8 ms |
5404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
617 ms |
250384 KB |
Output is correct |
2 |
Correct |
574 ms |
250384 KB |
Output is correct |
3 |
Correct |
564 ms |
250384 KB |
Output is correct |
4 |
Correct |
348 ms |
250384 KB |
Output is correct |
5 |
Correct |
606 ms |
250384 KB |
Output is correct |
6 |
Correct |
595 ms |
250384 KB |
Output is correct |
7 |
Correct |
576 ms |
250384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
3 ms |
2792 KB |
Output is correct |
3 |
Correct |
12 ms |
2792 KB |
Output is correct |
4 |
Correct |
3 ms |
2792 KB |
Output is correct |
5 |
Correct |
3 ms |
2860 KB |
Output is correct |
6 |
Correct |
4 ms |
2860 KB |
Output is correct |
7 |
Correct |
8 ms |
5196 KB |
Output is correct |
8 |
Correct |
8 ms |
5200 KB |
Output is correct |
9 |
Correct |
7 ms |
5324 KB |
Output is correct |
10 |
Correct |
9 ms |
5380 KB |
Output is correct |
11 |
Correct |
7 ms |
5404 KB |
Output is correct |
12 |
Correct |
8 ms |
5404 KB |
Output is correct |
13 |
Correct |
617 ms |
250384 KB |
Output is correct |
14 |
Correct |
574 ms |
250384 KB |
Output is correct |
15 |
Correct |
564 ms |
250384 KB |
Output is correct |
16 |
Correct |
348 ms |
250384 KB |
Output is correct |
17 |
Correct |
606 ms |
250384 KB |
Output is correct |
18 |
Correct |
595 ms |
250384 KB |
Output is correct |
19 |
Correct |
576 ms |
250384 KB |
Output is correct |
20 |
Correct |
593 ms |
250384 KB |
Output is correct |
21 |
Correct |
342 ms |
250384 KB |
Output is correct |
22 |
Correct |
593 ms |
250384 KB |
Output is correct |
23 |
Correct |
401 ms |
251372 KB |
Output is correct |
24 |
Correct |
625 ms |
253504 KB |
Output is correct |
25 |
Correct |
529 ms |
255344 KB |
Output is correct |
26 |
Correct |
574 ms |
262848 KB |
Output is correct |
27 |
Correct |
618 ms |
264928 KB |
Output is correct |