#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 1e5 + 7;
const int B = 100 + 7;
const ll INF = (ll) 1e18 + 7;
int n, bread_have, pigs[N];
ll gain[N];
ll dp[N][B];
ll sol = -INF;
vector<int> g[N];
void compute(int a, int p) {
for (int i = 0; i <= bread_have; i++) {
dp[a][i] = 0;
}
gain[a] = 0;
for (auto &b : g[a]) {
if (b != p) {
gain[a] += pigs[b];
for (int i = 1; i <= bread_have; i++) {
dp[a][i] = max(dp[a][i], dp[b][i]);
}
}
}
for (int i = bread_have - 1; i >= 0; i--) {
dp[a][i + 1] = max(dp[a][i + 1], dp[a][i] + gain[a]);
}
}
void dfs(int a, int p = 0) {
for (auto &b : g[a]) {
if (b != p) {
dfs(b, a);
}
}
compute(a, p);
}
void reroot(int a, int p) {
for (int i = 0; i <= bread_have; i++) {
dp[a][i] = 0;
}
gain[a] = 0;
for (auto &b : g[a]) {
if (b != p) {
gain[a] += pigs[b];
for (int i = 1; i <= bread_have; i++) {
dp[a][i] = max(dp[a][i], dp[b][i]);
}
}
}
for (int i = bread_have - 1; i >= 0; i--) {
dp[a][i + 1] = max(dp[a][i + 1], dp[a][i] + gain[a]);
}
for (int i = 0; i <= bread_have; i++) {
dp[p][i] = 0;
}
gain[p] = 0;
for (auto &b : g[p]) {
gain[p] += pigs[b];
for (int i = 1; i <= bread_have; i++) {
dp[p][i] = max(dp[p][i], dp[b][i]);
}
}
for (int i = bread_have - 1; i >= 0; i--) {
dp[p][i + 1] = max(dp[p][i + 1], dp[p][i] + gain[p]);
}
}
void solve(int a, int p = 0) {
sol = max(sol, dp[a][bread_have]);
for (auto &b : g[a]) {
if (b != p) {
reroot(a, b);
solve(b, a);
reroot(b, a);
}
}
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
///freopen ("input.txt", "r", stdin);
cin >> n >> bread_have;
for (int i = 1; i <= n; i++) {
cin >> pigs[i];
}
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1);
solve(1);
cout << sol << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2628 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2628 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
4 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
15 ms |
3580 KB |
Output is correct |
10 |
Correct |
5 ms |
3572 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
4 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
295 ms |
93300 KB |
Output is correct |
2 |
Correct |
297 ms |
95148 KB |
Output is correct |
3 |
Execution timed out |
4032 ms |
93224 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2628 KB |
Output is correct |
5 |
Correct |
1 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
4 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
15 ms |
3580 KB |
Output is correct |
10 |
Correct |
5 ms |
3572 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
4 ms |
3540 KB |
Output is correct |
13 |
Correct |
295 ms |
93300 KB |
Output is correct |
14 |
Correct |
297 ms |
95148 KB |
Output is correct |
15 |
Execution timed out |
4032 ms |
93224 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |