#include <bits/stdc++.h>
#define ll long long
#define all(aaa) aaa.begin(), aaa.end()
using namespace std;
const ll INF = 1e18;
const int N = 1e5 + 5, M = 105;
int a[N];
vector<int> g[N];
ll dp1[N][M], dp2[N][M][2], sum[N], dp3[M];
int n, m;
void upd(ll &a, ll b) {
a = max(a, b);
}
ll ans = 0;
void dfs(int node, int anc) {
if (anc != -1)
g[node].erase(find(all(g[node]), anc));
for (int to : g[node]) {
sum[node] += a[to];
dfs(to, node);
}
dp1[node][1] = sum[node];
dp2[node][1][1] = sum[node];
dp2[node][0][1] = -INF;
ll up = (anc == -1 ? 0 : a[anc]);
ans = max(ans, sum[node] + up);
for (int k = 0; k < 2; k++) {
memset(dp3, 0, sizeof(dp3));
for (int to : g[node]) {
for (int i = 0; i <= m; i++) {
ll cur = dp3[m - i] + dp1[to][i];
upd(ans, cur);
}
for (int i = 0; i <= m; i++) {
for (int j = 0; j < 2; j++) {
ll cur = dp2[to][i][j] + (j ? a[node] : 0);
upd(dp3[i], cur);
if (i < m) {
upd(dp3[i + 1], sum[node] - a[to] + up + cur);
}
}
}
for (int i = 1; i <= m; i++) {
upd(dp3[i], dp3[i - 1]);
}
}
reverse(all(g[node]));
}
for (int to : g[node]) {
for (int i = 0; i <= m; i++) {
upd(dp1[node][i], dp1[to][i]);
if (i < m) {
upd(dp1[node][i + 1] , dp1[to][i] + sum[node]);
upd(ans, dp1[to][i] + sum[node] + up);
}
for (int j = 0; j < 2; j++) {
ll cur = dp2[to][i][j] + (j ? a[node] : 0);
upd(dp2[node][i][0], cur);
if (i < m)
upd(dp2[node][i + 1][1], cur + sum[node] - a[to]);
}
}
}
for (int i = 1; i <= m; i++) {
upd(dp2[node][i][0], dp2[node][i - 1][0]);
upd(dp2[node][i][1], dp2[node][i - 1][1]);
upd(dp1[node][i], dp1[node][i - 1]);
}
upd(ans, dp2[node][m][0]);
upd(ans, dp2[node][m][1]);
upd(ans, dp1[node][m]);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
if (m == 0) {
cout << "0";
return 0;
}
dfs(0, -1);
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
10 ms |
5368 KB |
Output is correct |
8 |
Correct |
6 ms |
5240 KB |
Output is correct |
9 |
Correct |
6 ms |
5240 KB |
Output is correct |
10 |
Correct |
9 ms |
5240 KB |
Output is correct |
11 |
Correct |
7 ms |
5240 KB |
Output is correct |
12 |
Correct |
7 ms |
5240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
678 ms |
260972 KB |
Output is correct |
2 |
Correct |
757 ms |
261084 KB |
Output is correct |
3 |
Correct |
676 ms |
253144 KB |
Output is correct |
4 |
Correct |
298 ms |
253688 KB |
Output is correct |
5 |
Correct |
642 ms |
253724 KB |
Output is correct |
6 |
Correct |
659 ms |
253816 KB |
Output is correct |
7 |
Correct |
671 ms |
253708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
4 ms |
2680 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
10 ms |
5368 KB |
Output is correct |
8 |
Correct |
6 ms |
5240 KB |
Output is correct |
9 |
Correct |
6 ms |
5240 KB |
Output is correct |
10 |
Correct |
9 ms |
5240 KB |
Output is correct |
11 |
Correct |
7 ms |
5240 KB |
Output is correct |
12 |
Correct |
7 ms |
5240 KB |
Output is correct |
13 |
Correct |
678 ms |
260972 KB |
Output is correct |
14 |
Correct |
757 ms |
261084 KB |
Output is correct |
15 |
Correct |
676 ms |
253144 KB |
Output is correct |
16 |
Correct |
298 ms |
253688 KB |
Output is correct |
17 |
Correct |
642 ms |
253724 KB |
Output is correct |
18 |
Correct |
659 ms |
253816 KB |
Output is correct |
19 |
Correct |
671 ms |
253708 KB |
Output is correct |
20 |
Correct |
654 ms |
255828 KB |
Output is correct |
21 |
Correct |
67 ms |
8440 KB |
Output is correct |
22 |
Correct |
657 ms |
255840 KB |
Output is correct |
23 |
Correct |
309 ms |
255756 KB |
Output is correct |
24 |
Correct |
658 ms |
255852 KB |
Output is correct |
25 |
Correct |
689 ms |
254972 KB |
Output is correct |
26 |
Correct |
638 ms |
263136 KB |
Output is correct |
27 |
Correct |
612 ms |
263216 KB |
Output is correct |