#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100005;
const int V = 101;
int p[N];
int n, v;
vector<int> adj[N];
struct comp {
bool operator () (const array<ll,2>& ar1, const array<ll,2>& ar2) const {
return ar1 > ar2;
}
};
bool comp_fn (const array<ll,2>& ar1, const array<ll,2>& ar2) {
return ar1 > ar2;
}
array<ll, 2> dp[N][2][V][2];
void insert(int node, int typ, int bread, array<ll, 2> ar) {
if (dp[node][typ][bread][0][1] == 0) {
dp[node][typ][bread][0] = ar;
} else if (dp[node][typ][bread][1][1] == 0) {
dp[node][typ][bread][1] = ar;
} else {
if (ar > dp[node][typ][bread][1]) dp[node][typ][bread][1] = ar;
if (dp[node][typ][bread][1] > dp[node][typ][bread][0]) dp[node][typ][bread][0] = dp[node][typ][bread][1];
}
}
void dfs(int node, int par) {
ll val1 = 0;
for (int next : adj[node]) {
if (next == par) continue;
val1 += p[next];
dfs(next, node);
}
insert(node, 1, 1, {val1, node});
for (int next : adj[node]) {
if (next == par) continue;
for (int i = 0; i <= v; i++) {
insert(node, 0, i, {max((dp[next][0][i][0])[0], (dp[next][1][i][0])[0]), next});
if (i != v) insert(node, 1, i + 1, {max(val1 + (dp[next][0][i][0])[0], val1 + (dp[next][1][i][0])[0]), next});
}
}
for (int i = 0; i <= v; i++) {
for (int j = 0; j < 2; j++) insert(node, j, i, {0, node});
}
}
void transferRoot(int first, int second) {
ll defaultValue = 0;
for (int next : adj[second]) {
if (next == first) continue;
defaultValue += p[next];
}
for (int i = 0; i <= v; i++) {
array<ll, 2> it0 = dp[first][0][i][0];
array<ll, 2> it1 = dp[first][1][i][0];
if ((it0)[1] == second) dp[first][0][i][1];
if ((it1)[1] == second) dp[first][1][i][1];
ll val0 = 0;
ll val1 = 0;
if (it0[1] != 0) val0 = it0[0];
if (it1[1] != 0) val1 = it1[0];
insert(second, 0, i, {max(val0, val1 - p[second]), first});
if (i != v) insert(second, 1, i + 1, {max(val0 + defaultValue, val1 - p[second] + defaultValue), first});
}
for (int i = 1; i <= v; i++) {
if (dp[second][1][i][0][1] != 0) {
dp[second][1][i][0][0] += p[first];
}
if (dp[second][1][i][1][1] != 0) {
dp[second][1][i][1][0] += p[first];
}
}
}
ll result = 0;
void dfsRoot(int node, int par) {
for (int i = 0; i <= v; i++) {
for (int j = 0; j < 2; j++) {
result = max(result, (dp[node][j][i][0])[0]);
}
}
//cout << node << " " << result << endl;
/*
for (int next : adj[node]) {
if (next == par) continue;
transferRoot(node, next);
dfsRoot(next, node);
} */
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> v;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(1, -1);
dfsRoot(1, -1);
cout << result << "\n";
}
/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/
Compilation message
chase.cpp: In function 'void transferRoot(int, int)':
chase.cpp:71:50: warning: statement has no effect [-Wunused-value]
71 | if ((it0)[1] == second) dp[first][0][i][1];
| ~~~~~~~~~~~~~~~~~^
chase.cpp:72:50: warning: statement has no effect [-Wunused-value]
72 | if ((it1)[1] == second) dp[first][1][i][1];
| ~~~~~~~~~~~~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
711 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |