This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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, -1)));
roottree(0, -1);
printf("%lld", bigdp(0, v));
return 0;
}
}
Compilation message (stderr)
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);
~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |