# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
120156 | 2019-06-23T15:15:53 Z | luciocf | Dostavljač (COCI18_dostavljac) | C++14 | 346 ms | 2488 KB |
#include <bits/stdc++.h> using namespace std; const int maxn = 510; int n, m; int A[maxn]; // 0 - ending inside subtree // 1 - coming back to vertex int dp[2][maxn][maxn]; vector<int> grafo[maxn]; void dfs(int u, int p) { for (int i = 1; i <= m; i++) dp[0][u][i] = dp[1][u][i] = A[u]; for (auto v: grafo[u]) { if (v == p) continue; dfs(v, u); for (int i = m; i >= 1; i--) { for (int j = 0; j <= i; j++) { if (i >= j+1) dp[0][u][i] = max(dp[0][u][i], max(dp[0][v][j], dp[1][v][j]) + dp[1][u][i-j-1]); if (i >= j+2) { dp[0][u][i] = max(dp[0][u][i], dp[1][v][j] + dp[0][u][i-j-2]); dp[1][u][i] = max(dp[1][u][i], dp[1][v][j] + dp[1][u][i-j-2]); } } } } } int main(void) { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); grafo[u].push_back(v); grafo[v].push_back(u); } dfs(1, 0); printf("%d\n", max(dp[0][1][m], dp[1][1][m])); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 448 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 384 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 512 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 2 ms | 640 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 768 KB | Output is correct |
2 | Correct | 5 ms | 768 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 5 ms | 896 KB | Output is correct |
2 | Correct | 49 ms | 888 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 91 ms | 1072 KB | Output is correct |
2 | Correct | 30 ms | 1188 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 32 ms | 1580 KB | Output is correct |
2 | Correct | 205 ms | 1592 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 177 ms | 1948 KB | Output is correct |
2 | Correct | 73 ms | 1880 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 346 ms | 2392 KB | Output is correct |
2 | Correct | 35 ms | 2488 KB | Output is correct |