# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
310431 | pedroslrey | Chase (CEOI17_chase) | C++14 | 4035 ms | 98808 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 <bits/stdc++.h>
using namespace std;
using lli = long long int;
const int MAXN = 100010;
const int MAXM = 110;
#define debug(args...) //fprintf(stderr, args);
int n, m;
lli ps[MAXN];
lli dp[MAXN][MAXM];
vector<int> graph[MAXN];
void dfs(int v, int p) {
lli sum = 0;
for (int u: graph[v]) {
if (u == p) continue;
sum +=ps[u];
dfs(u, v);
}
for (int k = 0; k <= m; k++) {
dp[v][k] = 0;
for (int u: graph[v]) {
if (u == p) continue;
dp[v][k] = max(dp[v][k], dp[u][k]);
if (k) dp[v][k] = max(dp[v][k], dp[u][k - 1] + sum);
}
}
debug("dp[%d][%d] = %d\n", v, m, dp[v][m]);
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%lld", &ps[i]);
for (int i = 0; i < n-1; ++i) {
int u, v;
scanf("%d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
lli ans = 0;
for (int i = 1; i <= n; ++i) {
dfs(i, i);
debug("OI\n");
for (int j = 1; j <= n; ++j)
ans = max(ans, dp[j][m]);
}
printf("%lld\n", ans);
}
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... |