#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n, v;
vector<long long int> p;
vector< vector<int> > adjlist;
vector< vector<int> > children;
vector< vector< vector<long long int> > > dptable;
long long int smalldp(int v, int pa, int crumbs) {
if (dptable[v][pa][crumbs] != -1) return dptable[v][pa][crumbs];
if (crumbs == 0) return 0;
long long int ans = 0, maxdp = 0, maxnocrumb = 0;
for (int i = 0; i < adjlist[v].size(); i++) {
if (adjlist[v][i] == pa) continue;
ans += p[adjlist[v][i]];
maxdp = max(maxdp, smalldp(adjlist[v][i], v, crumbs - 1));
maxnocrumb = max(maxnocrumb, smalldp(adjlist[v][i], v, crumbs));
}
return dptable[v][pa][crumbs] = max(maxdp + ans, maxnocrumb);
}
void roottree(int v, int pa) {
for (int i = 0; i < adjlist[v].size(); i++) {
if (adjlist[v][i] != pa) {
children[v].push_back(adjlist[v][i]);
roottree(adjlist[v][i], v);
}
}
}
long long int bigdp(int v, int crumbs) {
if (dptable[0][v][crumbs] != -1) return dptable[0][v][crumbs];
if (crumbs == 0) return 0;
long long int ans = 0, maxdp = 0, maxnocrumb = 0;
for (int i = 0; i < children[v].size(); i++) {
ans += p[children[v][i]];
maxdp = max(maxdp, bigdp(children[v][i], crumbs - 1));
maxnocrumb = max(maxnocrumb, bigdp(children[v][i], crumbs));
}
return dptable[0][v][crumbs] = max(maxdp + ans, maxnocrumb);
}
int main() {
scanf("%d %d", &n, &v);
long long int nextint;
for (int i = 0; i < n; i++) {
scanf("%lld", &nextint);
p.push_back(nextint);
adjlist.push_back(vector<int>());
}
int a, b;
for (int i = 1; i < n; i++) {
scanf("%d %d", &a, &b);
adjlist[a - 1].push_back(b - 1);
adjlist[b - 1].push_back(a - 1);
}
if (n <= 1000) {
for (int i = 0; i < n; i++) {
dptable.push_back(vector< vector<long long int> >(n + 1, vector<long long int>(v + 1, -1)));
}
long long int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, smalldp(i, n, v));
}
printf("%lld\n", ans);
return 0;
} else {
for (int i = 0; i < n; i++) {
children.push_back(vector<int>());
}
dptable.push_back(vector< vector<long long int> >(n, vector<long long int>(v, -1)));
roottree(0, -1);
printf("%lld", bigdp(0, v));
return 0;
}
}
Compilation message
chase.cpp: In function 'long long int smalldp(int, int, int)':
chase.cpp:14:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjlist[v].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
chase.cpp: In function 'void roottree(int, int)':
chase.cpp:23:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adjlist[v].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~
chase.cpp: In function 'long long int bigdp(int, int)':
chase.cpp:34:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < children[v].size(); i++) {
~~^~~~~~~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &v);
~~~~~^~~~~~~~~~~~~~~~~
chase.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &nextint);
~~~~~^~~~~~~~~~~~~~~~~~
chase.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Runtime error |
486 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
256 ms |
95104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Correct |
2 ms |
256 KB |
Output is correct |
6 |
Correct |
2 ms |
256 KB |
Output is correct |
7 |
Runtime error |
486 ms |
525312 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
8 |
Halted |
0 ms |
0 KB |
- |