#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+10, MAXV = 101;
int n, vMax;
int p[MAXN];
vector<int> g[MAXN];
int a[MAXN], b[MAXN];
ll dp[MAXN][MAXV][3];
ll f(int curEdge, int v, int dir) {
if (v == 0) return 0;
ll &ret = dp[curEdge][v][dir];
if (ret != -1) return ret;
ret = 0;
int cur, prev;
if (dir == 0) cur = curEdge, prev = -1;
else if (dir == 1) prev = a[curEdge], cur = b[curEdge];
else cur = a[curEdge], prev = b[curEdge];
int sumAll = 0;
for (int edge : g[cur]) {
int nx = a[edge] == cur ? b[edge] : a[edge];
if (nx != prev) {
sumAll += p[nx];
}
}
for (int edge : g[cur]) {
int nx = a[edge] == cur ? b[edge] : a[edge];
if (nx != prev) {
int dir = (cur == a[edge] ? 1 : 2);
ret = max(ret, max(f(edge, v, dir),
f(edge, v-1, dir) + sumAll));
}
}
// printf("dp %-2d %-2d %-2d == %lld\n", curEdge, v, dir, ret);
// printf("dp: cur %-2d prev %-2d v %-2d == %lld\n", cur, prev, v, ret);
return ret;
}
int main() {
memset(dp, -1, sizeof(dp));
scanf("%d%d", &n, &vMax);
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
for (int i = 1; i < n; i++) {
scanf("%d%d", &a[i], &b[i]);
g[a[i]].push_back(i);
g[b[i]].push_back(i);
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans = max(ans, f(i, vMax, 0));
}
printf("%lld\n", ans);
}
Compilation message
chase.cpp: In function 'int main()':
chase.cpp:50:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &vMax);
~~~~~^~~~~~~~~~~~~~~~~~~
chase.cpp:51:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; i++) scanf("%d", &p[i]);
~~~~~^~~~~~~~~~~~~
chase.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i], &b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
239868 KB |
Output is correct |
2 |
Correct |
211 ms |
239984 KB |
Output is correct |
3 |
Incorrect |
182 ms |
239984 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
239868 KB |
Output is correct |
2 |
Correct |
211 ms |
239984 KB |
Output is correct |
3 |
Incorrect |
182 ms |
239984 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
4101 ms |
251932 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
296 ms |
239868 KB |
Output is correct |
2 |
Correct |
211 ms |
239984 KB |
Output is correct |
3 |
Incorrect |
182 ms |
239984 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |