# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
35789 | ainta | Chase (CEOI17_chase) | C++14 | 316 ms | 188392 KiB |
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, K, X[101000];
long long S[101000], D1[101000][110], D2[101000][110], T1[110], T2[110], Res;
vector<int>E[101000];
void DFS(int a, int pp) {
int i, x, j, k;
D1[a][1] = S[a];
for (i = 0; i < E[a].size(); i++) {
x = E[a][i];
if (x == pp)continue;
DFS(x, a);
for (j = 1; j <= K; j++) {
long long d1 = max(D1[x][j], D1[x][j - 1] + S[a] - X[x]), d2 = max(D2[x][j], D2[x][j - 1] + S[x] - X[a]);
T1[j] = d1, T2[j] = d2;
if (j != K) {
Res = max(Res, max(D1[a][K - j] + d2, D2[a][K - j] + d1));
Res = max(Res, D2[x][j] + S[a]);
}
}
for (j = 1; j <= K; j++) {
D1[a][j] = max(D1[a][j], T1[j]);
D2[a][j] = max(D2[a][j], T2[j]);
Res = max(Res, D1[a][j]);
}
}
}
int main() {
int i, a, b;
scanf("%d%d", &n, &K);
for (i = 1; i <= n; i++) {
scanf("%d", &X[i]);
}
for (i = 1; i < n; i++) {
scanf("%d%d", &a, &b);
E[a].push_back(b);
E[b].push_back(a);
S[a] += X[b], S[b] += X[a];
}
if (!K) {
printf("0\n");
return 0;
}
DFS(1, 0);
printf("%lld\n", Res);
}
Compilation message (stderr)
# | 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... |