# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
83392 | charlies_moo | Dostavljač (COCI18_dostavljac) | C++17 | 332 ms | 2652 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 <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 500;
const int M = 500;
typedef vector<int> vi;
int dp1[N][M+1], dp2[N][M+1];
void dfs(vi e[], int a[], int x, int p, int m) {
for (int i = 1; i <= m; i++) {
dp1[x][i] = dp2[x][i] = a[x];
}
for (int i = 0; i < e[x].size(); i++) {
if (e[x][i] == p) {
continue;
}
dfs(e, a, e[x][i], x, m);
for (int j = m; j >= 0; j--) {
for (int k = 0; k <= j - 1; k++) {
if (j - k - 2 >= 0) {
dp1[x][j] = max(dp1[x][j], dp1[x][j-k-2] + dp1[e[x][i]][k]);
dp2[x][j] = max(dp2[x][j], dp2[x][j-k-2] + dp1[e[x][i]][k]);
}
dp2[x][j] = max(dp2[x][j], dp1[x][j-k-1] + dp2[e[x][i]][k]);
}
}
}
// printf("%d:\n", x);
// for (int i = 0; i <= m; i++) {
// printf("%d %d %d\n", i, dp1[x][i], dp2[x][i]);
// }
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
int a[n];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
vi e[n];
for (int i = 0; i < n - 1; i++) {
int u, v;
scanf("%d %d", &u, &v);
u--, v--;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(e, a, 0, -1, m);
printf("%d\n", dp2[0][m]);
return 0;
}
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... |
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |